Stopping all player interactions with OnCollisionEnter?

Updated on February 22, 2018 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on February 12, 2018

Hi

 

Basically I want my object that is controlled by getKeyUp to ignore key presses once it hits another object while still maintaining all physics attributes ie the player can no longer control said object but the object can move based off of physics from the environment.

As many people suggest making it kinematic or locking all movement but thats not how I want my game to work.

Any Ideas?

Thanks

  • Liked by
Reply
0 on February 12, 2018

There are 2 ways you could do this pretty easily.

 

1. Just have a bool, canRecievInput, an if its false, break out of the update loop before any input is recorded.

bool canRecieveInput = true;
 
void Update()
{
    if(canRecieveInput == false) return; // This will prevent any further code in Update from happening
 
        // your current update loop would go here
}
 
void OnCollisionEnter()
{
    canRecieveInput = false;
}

 

2. If your movement has a speed variable, just set it to 0.

float speed = 10;
void Update()
{
    if(Input.GetKey(KeyCode.Space))
    {
        transform.Translate(transform.forward * Time.deltaTime * speed); // this will move you "speed" units per second while holding space.
    }
}
 
void OnCollisionEnter()
{
    speed = 0; // so if speed is 0, you will be moving 0 units per second
}
  • Liked by
Reply
Cancel