- Home /
Question by
ELITEMASTER321 · Apr 16, 2020 at 11:20 AM ·
rigidbodyaddforcetarget
How do you use add force to move a rigidbody to a transform.position?
Im trying to make a gun that when the trigger on the end of it touches a target it will pull it to the front of the gun kind of like a vacuum would. I’m struggling to find a way to use Rigidbody.Addforce to move the rigidbody to a transforms position. Thanks for any help in advance. :) Kind Regards, ELITEMASTER321
Comment
Best Answer
Answer by qobion · Apr 16, 2020 at 11:44 AM
You should apply forces in fixed update. Use ForceMode.VelocityChange or modify velocity directly
public Rigidbody[] items;
public float force;
void FixedUpdate()
{
foreach (Rigidbody item in items)
{
Vector3 direction = transform.position - item.transform.position;
item.AddForce(direction.normalized * force, ForceMode.VelocityChange);
}
}
Thanks so much qobion, I’d been trying to do this all day. :)
Just one more question, How do I add the rigidbody that I'm shooting at to the array so that the force can be applied to it? Thanks,