Question by
parkerp1000 · Sep 16, 2015 at 04:53 PM ·
rotationmovementcharacter
Making Character Rotate
Hello, I am making a game for a class and I am trying to make a character rotate to the left right or back if I push the left right or down button to make it easier to follow along with the game. Inserted below is my movement script I have so far.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private float turningSpeed = 20f;
private Rigidbody playerRigidbody;
private Vector3 movement;
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);
}
void FixedUpdate () {
//If the players movement vector does not = 0...
if (movement != Vector3.zero) {
// then create a target rotation based on the movement vector
Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up);
//.... and create another rotation that moves from the current rotation to the taget rotation...
Quaternion newRotation = Quaternion.Lerp (playerRigidbody.rotation, targetRotation, turningSpeed * Time.deltaTime);
//...and change the players rotation to the new incremental rotation
playerRigidbody.MoveRotation (newRotation);
}
}
}
`
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612033958im_/https://answers.unity.com/themes/thub/images/avi.jpg)