- Home /
Problem with OnCollisionStay and user input
Hi
I have this code where I want to give force to ball if it rest on a box and user press space. It works if I press space before ball hits the box, but if ball rest on box and I press space nothing happens.
This is script in box gameobject:
function OnCollisionStay (collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
if (Input.GetButton ("Space")) {
contact.otherCollider.rigidbody.AddForce( -contact.normal * Force, ForceMode.Impulse);
}
}
Answer by rutter · May 16, 2012 at 08:56 PM
if ball rest on box and I press space nothing happens
Probably the first thing I'd check is this: is OnCollisionStay()
still being called in that circumstance? Easiest way to check this is with Debug.Log().
If it is, then you know something in your own function isn't working as expected.
If it isn't, then you know that hook won't work for you, and you'll need to make a more substantial change, either with your collision setup or with the way you check if the ball is "on" the box.
Answer by khellstr · May 16, 2012 at 10:55 PM
Problem was that rigidbody of ball falls asleep, and collision is no longer detected.
this is code I get to work:`var bumperForce : float = 100;
var shoot:boolean = false;
var lastCollision : GameObject;
var normal : Vector3;
function FixedUpdate () {
if (Input.GetButton ("Space") & shoot) {
lastCollision.rigidbody.AddForce( normal * bumperForce, ForceMode.Impulse);
shoot = false;
}
}
function OnCollisionEnter(collision : Collision) {
lastCollision = collision.gameObject;
for (var contact : ContactPoint in collision.contacts) {
shoot = true;
normal = -contact.normal;
}
} `
Your answer
Follow this Question
Related Questions
Need help with collision detection 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
In-game pickup not moving towards player after being collected 1 Answer
How do I make OnTriggerEnter() respond to just one collider? 2 Answers
Script is stuck in OnCollisionStay 1 Answer