- Home /
Using a trigger to align an object?
I have very little coding experience. I have searched and tried several different methods that don't seem to do what I want.
Here is what I'm trying to do:
Create an align or snap effect.
There is an invisible trigger and an object being dragged with physics. When the object is dragged through the trigger I would like the dragged object to adopt the triggers exact position (BUT NOT rotation and scale) and stop the drag.
I have done several tutorials for learning Unity script and have been able to figure out everything else that I have needed up to this point. I won't give up on learning it, but at this point I need some help.
Thank you in advance!
Answer by aldonaletto · Aug 30, 2011 at 12:08 AM
Basically, you can force the rigidbody to the trigger's position in OnTriggerEnter. But there are some problems you must handle: you should also zero the rigidbody's velocity and angularVelocity, or it will continue moving, and you should also keep the original vertical coordinate. Supposing the ground is plane, and Y is the vertical axis, you can do the following:
function OnTriggerEnter(col: Collider){ SnapObject(col.transform); }
function SnapObject(obj: Transform){ if (obj.rigidbody){ obj.rigidbody.velocity = Vector3.zero; obj.rigidbody.angularVelocity = Vector3.zero; } obj.position.x = transform.position.x; obj.position.z = transform.position.z; }
Your answer
Follow this Question
Related Questions
Collission and Trigger on Same object 0 Answers
Trigger if 3 Objects Collide at the same time 0 Answers
overlapsphere to destroy NPCs on exit 1 Answer
OnTriggerEnter Not Working 3 Answers
Collision detection accuracy issues. 1 Answer