So, I have this script to rotate my character in the direction he’s moving in(currently forward or backwards). I have another script that uses mouse input to rotate the character globally and move the character. And the rotation script is parented to the global rotation and movement script. But when I try to rotate the character locally, he rotates to the global 180 or 0 and the inspector shows rotations like 130, 55 etc depending on the global rotation.
Rotation script:
public class playerRot : MonoBehaviour {
public float smooth = 15.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Quaternion targetf = Quaternion.Euler(0,180, 0); // Vector3 Direction when facing frontway
Quaternion targetb = Quaternion.Euler(0, 0, 0); // Vector3 Direction when facing opposite way
if (Input.GetAxisRaw("Vertical") < 0.0f)
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetf, Time.deltaTime * smooth);
}
if (Input.GetAxisRaw("Vertical") > 0.0f)
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetb, Time.deltaTime * smooth);
}
}
}
Main script:
sing UnityEngine;
using System.Collections;
public class basicMovement : MonoBehaviour {
private Animator anim;
private CharacterController controller;
private float speed = 6.0f;
public float gravity = 20.0f;
public float jumpSpeed = 100.0f;
public float runSpeed = 20.0f;
public float walkSpeed = 6.0f;
public bool running = false;
public bool InAir = false;
private Vector3 moveDirection = Vector3.zero;
private Vector3 sideDirection = Vector3.zero;
public bool paused = false;
public float turnSpeed = 60.0f;
public float mouseSensitivity = 1.5f;
public float smooth = 15.0f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
controller = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.W)) {
anim.SetInteger ("AnimPar", 1);
running = true;
if (Input.GetKey (KeyCode.LeftShift)) {
anim.SetInteger ("AnimPar", 2);
running = false;
} else {
running = true;
}
} else {
anim.SetInteger ("AnimPar", 0);
running = false;
if (Input.GetKey(KeyCode.S))
{
anim.SetInteger("AnimPar", 1);
running = true;
}
}
if (running) {
speed = runSpeed;
if (Input.GetKey(KeyCode.C))
{
speed = 5 * runSpeed;
anim.speed = 1.5f;
} else
{
anim.speed = 1;
}
} else {
anim.speed = 1;
speed = walkSpeed;
}
if (InAir == true)
{
if (running)
{
anim.SetInteger("AnimPar", 5);
}
}
if (controller.isGrounded) {
InAir = false;
moveDirection = transform.forward * Input.GetAxis ("Vertical") * speed;
sideDirection = transform.right * Input.GetAxis ("Horizontal") * speed;
if (Input.GetKeyDown (KeyCode.Space)) {
moveDirection.y = jumpSpeed;
}
if (Input.GetKeyUp (KeyCode.Space)) {
if (running == false) {
moveDirection.y = jumpSpeed;
InAir = true;
}
}
} else {
moveDirection.y -= gravity * Time.deltaTime;
InAir = true;
}
if (Input.GetKeyUp (KeyCode.Escape)) {
if (paused) {
paused = false;
} else {
paused = true;
}
}
if (paused) {
Time.timeScale = 0;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
} else {
Time.timeScale = 1;
float turn = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate (0, turn * turnSpeed * Time.deltaTime, 0);
moveDirection.y -= gravity * Time.deltaTime;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
controller.Move(moveDirection * Time.deltaTime + sideDirection * Time.deltaTime);
}
}
}
How would I get the child object to rotate based on it’s own rotation?