- Home /
Checking if touching a trigger.
Hi there It is quite simple to describe what i'm looking for, but i couldn't find a spezific answer for my problem.
Example:
if(//am i touching a trigger named "RightPlane"?//){
rigidbody.velocity.x+=2;
}
So what i have to type instead the of the comment to get this working? If it isnt possible this way, show me another solution. Help would be awesome, thanks for your attention =)
What you have here doesnt check for collision. That code is to move the object with velocity through the x axis.
Check out OnTriggerEnter() or OnCollisionEnter() to check for collision between two objects.
Answer by youngapprentice · Feb 08, 2013 at 06:19 PM
It would be
function OnTriggerEnter( collided : Collider ){
if( collided.transform.tag == "RightPlane" ){
rigidbody.velocity.x += 2;
}
}
There are some things you need to consider here first though. You might want to multiply a value by Time.deltaTime to get the addition of velocity frame independent.
Also, it is better to do physics things in FixedUpdate. An option here would be to set a bool to true in the TriggerEnter and then adding the velocity in FixedUpdate.
Also, it is usually bad to change the velocity directly. You should try using AddForce instead.
Some links:
http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.FixedUpdate.html http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html http://docs.unity3d.com/Documentation/Components/Tags.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613092349im_/https://answers.unity.com/themes/thub/images/avi.jpg)