0
votes

After rotating the character with jotstick. rotate resetting. direction of the character With the joystick, I want the character to look in that direction when I turn my hand in the direction I want and then pull my hand out of the joystick. Don't let him look in the same direction joysticki when I left. how can I do it. thanks.

public class MyJoystick : MonoBehaviour
{
    public Joystick joystick;
    public Joystick joystickRot;
    public float moveSpeed;
    Quaternion targetRotation;
    Rigidbody rigidbody;


    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();

    }

    // Update is called once per frame
    void Update()
    {
        // var rigidbody = GetComponent<Rigidbody>();


        rigidbody.velocity = new Vector3(joystick.Horizontal * moveSpeed, rigidbody.velocity.y, joystick.Vertical * moveSpeed);

        // this is problem
        // don't reset the rotate when joysticki is released.

        transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Atan2(joystickRot.Horizontal
              , joystickRot.Vertical) * Mathf.Rad2Deg, transform.eulerAngles.z);
    }
}
1

1 Answers

0
votes

'joystickRot' will return zeros when not being pushed in any other direction, that is why your rotation is being reset, so you must check first that the joystick is actually being used before applying the values to your GameObjects transform rotation.

I'm not overly familiar with the joystick system you are using, but there will most certainly be a way to test if the joystick is being used or not, and only apply the force and rotations when it is.

By the way, you have two Joystick objects referenced, but I believe you only need one.