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.
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;
}