H
H
Herman Coffman2019-11-20 17:57:13
C++ / C#
Herman Coffman, 2019-11-20 17:57:13

Rotation control with joystick, how?

Hello! Straight to the point. I have a dragon and with the help of a joystick I "drag" it around the scene, but since it is a 3d model and it has a face, it turns out that it moves sideways. I want him to go wherever he goes and turn around. And so that when you release the joystick, the object stops moving. Here is the code for the movement:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JoystickPlayerExample : MonoBehaviour
{
    public float speed;
    public VariableJoystick variableJoystick;
    public Rigidbody rb;
    //gameObject.transform.Rotate(0, 100 * Time.deltaTime ,0);
    public void FixedUpdate()
    {
        Vector3 direction = Vector3.forward * variableJoystick.Vertical + Vector3.right * variableJoystick.Horizontal;
        rb.AddForce(direction * speed * Time.fixedDeltaTime, ForceMode.VelocityChange);

    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2019-11-21
@xmoonlight

Well, what's the problem?
There is direction and there is .transform.Rotate - it remains to tie it together.

G
Griboks, 2019-11-22
@Griboks

In addition to the hardcore version, you can also consider the standard Input Axes. Look at the examples, it uses something like ) that works on both the gamepad and the keyboard. Input.GetAxis("horizontal"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question