Hi, I’m currently in the process of making a top down shooter and need to get the player to rotate fast when running and slowly when walking. I have The following code but I just don’t see why it is that it’s not working:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (CharacterController))]
public class PlayerController : MonoBehaviour
{
//Player Properties
public float rotationSpeedRun = 5000;
public float rotationSpeedWalk = 450;
public float walkSpeed = 5;
public float runSpeed = 8;
private Quaternion targetRotation;
private CharacterController controller;
// Use this for initialization
void Start ()
{
controller = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update ()
{
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
if (input != Vector3.zero)
{
targetRotation = Quaternion.LookRotation (input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle (transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeedWalk * Time.deltaTime);
}
else if (Input.GetButton ("Run")&&(input != Vector3.zero))
{
targetRotation = Quaternion.LookRotation (input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle (transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeedRun * Time.deltaTime);
}
Vector3 motion = input;
motion *= (Mathf.Abs (input.x) == 1 && Mathf.Abs (input.x) == 1) ? .7f : 1;
motion *= (Input.GetButton ("Run")) ? runSpeed : walkSpeed;
motion += Vector3.up * -8;
controller.Move (motion * Time.deltaTime);
}
}
All I get is the player always rotating at the slower speed even when running.
I bet it’s the simplest thing so a second pair of eyes to read over my player controller script would be awesome. Thanks!