- Home /
Face Direction Movement using CharacterController
Hi to all! I have a simple question about how rotate the character to face his movement direction.
It's a isometric project that uses CharacterController.Move script reference to move the player.
Ps.: is NOT a click-to-move or mouse pointer direction like most Survival-games, just a simple WASD/Joystick Axis based, and the camera is fixed too.
public class ExampleClass : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
How can I implement the rotation on Y-axis in this case and make the character face his direction??
Thanks for the attention!
Answer by Namey5 · Nov 22, 2016 at 09:40 AM
if (moveDirection != Vector3.zero)
transform.rotation = Quaternion.LookRotation (moveDirection);
You would place this inside the general Update function, rather than the .isGrounded if statement.
Thanks for the answer @Namey5
But, I did not understand what you mean... If I put this code Ins$$anonymous$$d of where checks if(.isGrounded), its some gliches, like dont move anymore or rotate very fast and cannot set a correct direction. I put after the line 15 to test, but the character faces only the ground.
Can you write code that you uses?
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Why the player is not rotating and moving at the same time ? 0 Answers
problem after rotate an object 1 Answer
How can I make it so cameraCenter Roatates 90 degrees smoohly after swipe! 1 Answer
Objects not rotating right half the time based on another object 0 Answers