Search Unity

Pushing object in right direction

Discussion in 'Scripting' started by Renofox, Jan 30, 2015.

  1. Renofox

    Renofox

    Joined:
    Jan 14, 2015
    Posts:
    10
    I'd like to have an invisible force pushing an object towards another one. I have the target in variable "GameObject target", but how do I make the force for moving towards it?
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Code (csharp):
    1. Vector3 PushDirection = (TargetToPushTowards.transform.position - ObjectToPush.transform.position);
    That will give you the direction to push. Normalize it and multiply by some value to get the force you want to apply.