I am actually trying to make the player Sprint by pressing the LeftShift key and Crouch by pressing the LeftControl Key.
I attached the C# Code at the bottom. So here, the Shift key works, because I tested by using Debug.Log("Sprint Triggered") when the key is pressed, and the Crouch key works too in the same way.
But, when I press Shift, the speed of the player doesn't change at all. And also, when I press crouch, the player just gives a really really quick jump up.
I have set all the variables correctly, I double checked them. But I don't know why there is no change in the motion of the body.
Let me know if you need any more details
public class SprintingCrouching : MonoBehaviour
{
private PlayerMovement playerMovement;
public float sprintSpeed = 10f;
public float moveSpeed = 5f;
public float crouchSpeed = 2f;
private Transform look_rot;
private float standHeight = 1.6f;
private float crouchHeight = 1f;
private bool isCrouching = false;
// Start is called before the first frame update
void Awake()
{
playerMovement = GetComponent<PlayerMovement>();
look_rot = transform.GetChild(0);
}
// Update is called once per frame
void Update()
{
SprintPlayer();
Crouch();
Debug.Log(playerMovement.speed);
}
void SprintPlayer()
{
if(Input.GetKeyDown(KeyCode.LeftShift))
{
Debug.Log("Sprint Triggered");
playerMovement.speed = sprintSpeed;
}
}
void Crouch()
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
Debug.Log("Crouch Triggered");
look_rot.localPosition = new Vector3(0f, standHeight, 0f);
playerMovement.speed = moveSpeed;
isCrouching = false;
}
else
{
look_rot.localPosition = new Vector3(0f, crouchHeight, 0f);
playerMovement.speed = moveSpeed;
isCrouching = true;
}
}
}
CodePudding user response:
The body is changing, just that its only in the moment you press down the key and then it returns to its normal state. You can make a function that checks for holding/releasing certain keys and modifies a bool variable that your sprint/crouch function will read, like this:
public class SprintingCrouching : MonoBehaviour
{
private PlayerMovement playerMovement;
public float sprintSpeed = 10f;
public float moveSpeed = 5f;
public float crouchSpeed = 2f;
private Transform look_rot;
private float standHeight = 1.6f;
private float crouchHeight = 1f;
private bool isCrouching = false, isSprinting = false;
// Start is called before the first frame update
void Awake()
{
playerMovement = GetComponent<PlayerMovement>();
look_rot = transform.GetChild(0);
}
// Update is called once per frame
void Update()
{
GetInput();
SprintPlayer();
Crouch();
Debug.Log(playerMovement.speed);
}
void GetInput()
{
if(Input.GetKeyDown(KeyCode.LeftShift)) isSprinting = true;
if(Input.GetKeyUp(KeyCode.LeftShift)) isSprinting = false;
if(Input.GetKeyDown(KeyCode.LeftControl)) isCrouching = true;
if(Input.GetKeyUp(KeyCode.LeftControl)) isCrouching = false;
//GetKeyUp detects when a key is released.
}
void SprintPlayer()
{
if(isSprinting)
{
Debug.Log("Sprint Triggered");
playerMovement.speed = sprintSpeed;
}
else
{
playerMovement.speed = moveSpeed;
}
}
void Crouch()
{
if(isCrouching)
{
Debug.Log("Crouch Triggered");
look_rot.localPosition = new Vector3(0f, standHeight, 0f);//Maybe this is supposed to be crouchHeight instead?(because you are enabling crouch)
playerMovement.speed = moveSpeed; //Maybe you tried to use crouchSpeed here instead?
}
else
{
look_rot.localPosition = new Vector3(0f, crouchHeight, 0f); //Maybe this is supposed to be standHeight(because it's disabling crouch)
playerMovement.speed = moveSpeed;
}
}
}
Also a note to make your code cleaner you can declare similar variables using a , like so:
int var1 = 3, var2 = 7;
CodePudding user response:
Hey ill help as i have made a few, movement systems with unity and c#
public Transform Body;
class Movement
{
public Transform Body;
crouchScale = new Vector3(0f, standHeight/2, 0f);
if(Input.getButtonDown(0)
{
Body.localPostion = new Vector3(0f, crouchScale, 0f);
}
else
{
return;
}