I want to add a sprint key while you hold Lshift you sprint and when you let go you stop sprinting.
I tried with this script and the player will sprint but after you let go of Lshift it will keep the speed at 10.
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float speed = 5f;
void FixedUpdate () {
float d = Input.GetAxis ("Horizontal");
float w = Input.GetAxis ("Vertical");
Vector3 moveVector = (transform.forward * w) + (transform.right * d);
moveVector *= speed * Time.deltaTime;
transform.localPosition += moveVector;
if (Input.GetKey (KeyCode.LeftShift)) {
speed = 10f;
else{
speed = 5f;
}
}
}
}
Hello! Not sure if you copy pasted your code, but there’s a bracket missing just before the else, which might be the problem.
Also I’d recommend using the Input.GetButton() function instead of GetKey(), since it allows for a more flexible key input. I don’t know, maybe you have your reasons for using GetKey().
public class PersController : MonoBehaviour
{
public float
speed,speedUsePerc,speedMax,sprint,stamina,dash,dashUse,dashKd,sprintUse,staminaKd,staminaRegen,staminaMax;
private float staminaT = 0,dashT = 0,staminaPercUp,speedPercE;
public Rigidbody rb;
private bool kdStaminaT = false;
private bool kdDashT = false;
float camRayLength = 100;
int floorMask;
public GUIText StaminaH;
public GUIText StaminaKD;
void Awake ()
{
floorMask = LayerMask.GetMask ("Floor");
}
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
void Update()
{
Move ();
StaminaH.text = "Stamina " + stamina;
StaminaKD.text = "" + staminaT;
speed = Mathf.Clamp (speed, 0, speedMax);
}
void FixedUpdate()
{
Turning ();
}
void Turning ()
{
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit floorHit;
if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
rb.MoveRotation (newRotation);
}
}
void Move()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal,0,moveVertical);
rb.velocity = movement * speed;
if (staminaT >= staminaKd)
{
kdStaminaT = false;
staminaT = 0;
}
else if (kdStaminaT == true)
{
staminaT += Time.deltaTime;
}
else if(stamina < staminaMax & staminaT >= staminaKd)
{
kdStaminaT = true;
}
if (dashT >= dashKd)
{
kdDashT = false;
}
else if (kdDashT == true)
{
dashT += Time.deltaTime;
}
if (stamina < staminaMax & movement == Vector3.zero & kdStaminaT == false)
{
stamina += staminaRegen * Time.deltaTime;
}
else if (stamina < staminaMax & movement != Vector3.zero & kdStaminaT == false)
{
stamina += staminaRegen / 2 * Time.deltaTime;
}
else if (speed > speedMax)
{
speed = speedMax;
}
else if (stamina > staminaMax)
{
stamina = staminaMax;
}
if (Input.GetKey (KeyCode.LeftShift) & stamina > 0 & movement != Vector3.zero)
{
rb.velocity = movement * sprint;
stamina -= sprintUse * Time.deltaTime;
kdStaminaT = true;
staminaT = 0;
}
if (Input.GetKeyDown (KeyCode.Space) & stamina > 0 & kdDashT == false)
{
rb.AddForce(movement * dash,ForceMode.Impulse);
stamina -= dashUse;
kdDashT = true;
dashT = 0;
}
if (Input.GetKey (KeyCode.LeftShift) & Input.GetKeyDown (KeyCode.Space) & stamina > 0 & kdDashT == false)
{
rb.AddForce(movement * (dash + speed),ForceMode.Impulse);
stamina -= dashUse * 0.5f;
kdDashT = true;
dashT = 0;
}
}
}
First line where all float values dont wont add in the code(
That script for a player, them have a sprint,dash,stamina,MOAR timers for cooldowns,stamina regenerate and MOAR MOAR MOAR.
Sorry for my clumsy english and i hope that will help you.)))