I’m simply trying to get the direction of the collision object in an OnTriggerStay2D so I can addforce to the object with the trigger collider in the same direction. I don’t want the velocity, just the direction. The closest I’ve come to getting it to work is collision.transform.right, but that only sends it right (obviously), and collision.transform.position seems to want to give me the magnitude and all. New at this, any help is appreciated. Thanks!
You need to compare the last position to the current position and that will give you the direction. Then when you do OnTriggerEnter2D you can use the direction that you know the object is moving in.
Something like this on the moving object:
Vector2 lastPosition;
public Vector2 MovementDirection;
void Update()
{
var currentPosition = new Vector2(transform.position.x, transform.position.y);
// don't calculate direction when we are standing still
if(currentPosition != lastPosition)
MovementDirection = (currentPosition - lastPosition).normalized;
lastPosition = currentPosition;
}
Then you can reference it when you want to add force in the direction you are moving
void OnTriggerEnter2D(Collider2D other)
{
MovingObject.MovementDirection .....
}
Awesome, I’ll give it a shot, thanks!