Hi, this is my first time using this. I’ll get straight to the point.
I’m using a PS4 Controller to control the player.
When I start to run it loops on the spot for a second and then moves along, its like the character controller can’t keep up with the speed of the run and walking works fine.
The last thing is that when ever I let go of the R2 button to stop running the character slides back, so how do I stop that from happening?
Thanks :^) Down Below is is the script for my player.
Animator anim;
CharacterController controller;
public float walkSpeed = 7f;
public float gravity = 50f;
public float rotationSpeed = 10f;
private float yRoat = 90;
public Transform body;
public float sensitivity = 2f;
public float runSpeed;
//________________________________________________________________//
void Start ()
{
anim = GetComponent<Animator> (); //Get the component for the animator
controller = GetComponent<CharacterController> ();
}//End OF start
//____________________________________________________________________//
void Update ()
{
float move = Input.GetAxis ("Vertical"); //to set the float in the animator
//variable called vertical, type vector3 = the transform's direction, move it forward on the z axis
Vector3 vertical = transform.TransformDirection (Vector3.forward);
Vector3 horizontal = transform.TransformDirection (Vector3.right);
Vector3 height = transform.TransformDirection (Vector3.up);
Vector3 L2Axis = transform.TransformDirection (Vector3.forward);
//
controller.Move((vertical * (walkSpeed * Input.GetAxis("Vertical"))) * Time.deltaTime);
controller.Move((horizontal * (walkSpeed * Input.GetAxis("Horizontal"))) * Time.deltaTime);
controller.Move ((L2Axis * (runSpeed * Input.GetAxis ("PS4_R2"))) * Time.deltaTime);
//yRoat += 5 * Input.GetAxis("Mouse X");
yRoat += 1.5f * Input.GetAxis("Horizontal") * sensitivity; //use the horizontal keys to rotate
transform.rotation = Quaternion.Euler (0, yRoat, 0); //rotate the player
controller.Move (height * -gravity * Time.deltaTime);
if(controller.isGrounded)//If player is grounded then move
{
anim.SetFloat ("Speed", move);
}else{
}
//If the left analouge stick axis is greater than 0 and the R2 axis is greater than 0 then do the following
if(Input.GetAxis("Vertical") > 0 && Input.GetButton("PS4_R2"))
{
float run = Input.GetAxis ("Vertical");
anim.SetBool("Run", true); //run is true
}else{
anim.SetBool ("Run", false); //run is false
anim.SetFloat ("Speed", move); //and walk again
}
}//End of Update