- Home /
Question by
Codeyellow_97 · Aug 20, 2016 at 09:41 AM ·
stopnewstoppingstops
How can i make an object brake when pushing a button?
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.
Comment
If the button has been pressed, set a bool to true. Then use the state of this bool to check wether any kind of movemenr or input registration occurs in FixedUpdate.
Answer by sourav13 · Aug 20, 2016 at 01:40 PM
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);
}
Answer by sohail11330 · Aug 20, 2016 at 02:13 PM
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);
if(Input.GetKey(KeyCode.Space ))
{
rb.isKinematic = true;
}
}
when the player is moving then just press space , the player will stop moving.