I’ve been learning how to do this for about a few months now and I’ve decided to try and do a project by scratch.I know this might simple to others, but I’m a beginner. Pretty much the player moves with this code:
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
All I want to know is how I can code the player to stop when a button is pressed, please help.
Try this declare a variable boolean and set it’s value to true.
Than in FixedUpdate check .
if(Input.getKeyDown()){
set your variable to false;
}else {
set it to true;
}
than
if(your variable){
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
if(Input.GetKey(KeyCode.Space ))
{
rb.isKinematic = true;
}
}