Question by
MarioTheDev · Sep 21, 2016 at 02:18 AM ·
controller
How to add a sprint button on a 3D platformer? In C#
I am making a 3D platformer and I can't figure out how to add a sprint to this script in C#. using UnityEngine; using System.Collections;
public class Controller : MonoBehaviour {
public float speed = 6.0F;
public float sprint = 8.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);
}
}
Comment
Best Answer
Answer by Namey5 · Sep 21, 2016 at 04:01 AM
public class Controller : MonoBehaviour {
public float speed = 6.0F;
public float sprint = 8.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);
if (Input.GetKey (KeyCode.Shift))
moveDirection *= speed * 2;
else
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Thank you very much 13. if (Input.Getkey ($$anonymous$$eyCode.LeftShift)) was the only thing wrong