A
A
Alex Kolyhov2020-05-13 17:20:58
C++ / C#
Alex Kolyhov, 2020-05-13 17:20:58

How to make a character rotate in the direction of his movement in Unity?

I am new to Unity. I am making an Android game. In the game, the camera is located above the character, and slightly to the side. I made it move (it's made for touch control), but I don't know how to make the character turn in the direction of his movement.
Here is the code I wrote to move it:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]

public class PlayerMoveController : MonoBehaviour {

  // PUBLIC
  public SimpleTouchController leftController;
  public float speedMove;

  // PRIVATE
  private Rigidbody _rigidbody;
  [SerializeField] bool continuousRightController = true;

  void Awake()
  {
    _rigidbody = GetComponent<Rigidbody>();
  }

  public bool ContinuousRightController
  {
    set{continuousRightController = value;}
  }

  void FixedUpdate()
  {
    // move
    _rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMove) +
      (transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMove) );

  }

}


Can you please tell me how to implement my idea?
I worked on his movement and turns for 2 days, but he either turned and did not walk, or vice versa.
I will be very grateful : )

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question