- Home /
onCollisioStay only for specific objects
I have to make my charachter jump only if he touches a collider of a specific layer (or tag): How can I do?
This is my script:
var sp = 200.0; var jump = 200.0;
function Update () { rigidbody.AddForce (Input.GetAxis("Horizontal")*sp*Time.deltaTime, 0, 0); }
function OnCollisionStay(collisionInfo : Collision) {
if (Input.GetButton ("Jump")) {
rigidbody.AddForce (0, jump, 0);
}
}
Answer by efge · Mar 19, 2011 at 03:05 PM
For using tags you could use the information calculated by Collision:
function OnCollisionStay(collisionInfo : Collision) {
if (Input.GetButton ("Jump") && collisionInfo.collider.tag == "XY") {
rigidbody.AddForce (0, jump, 0);
}
}
The Layer-Based Collision Detection is described here.
Your answer
Follow this Question
Related Questions
Is there an easy way to apply the same tag to all children of an object? 4 Answers
change layer of child 5 Answers
Can't add custom tag/layer. It just turn back to 'Default' 1 Answer
Unity Raycasting a tag rather than layer 1 Answer
Is this workaround OK? It's for fixing lost tag/layer info on export/import 1 Answer