Rotate an object from 2 Vector2s?

Hey there,

I have an object that can pivot an arbitrary amount (lets say 90°).

I need to rotate it relative to the initial touch position (A Vector2) and the ongoing touch position (also a Vector2). Is there some way I can calculate the new angle between these points and have the rotation follow them accordingly?

Thanks

Okay Sounds good. Thanks for the information. Here’s what I recommend. If you like please accept the answer.

  var myAngle : float = Vector2.Angle(initialTouchPos, ongoingTouchPos);

This will return the angle as a floating point variable named myAngle. This can be slapped onto an object however you see fit. If you’re just trying to manipulate the point which it sounds like.

  Vector2.MoveTowards(initialTouchPos, ongoingTouchPos);  //Rotates the instantly to the new heading to follow the finger

If you are looking to rotate it smoothly between the two points you could do something like this.

  var rotation = Quaternion.LookRotation(ongoingTouchPos - initialTouchPos);
  transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);

This will give you a smooth rotation between ongoingTouchPos and initialTouchPos. The original code I gave you should give the angle between two points relative to the transform that calls it. Not world zero unless your transform is at world zero. :wink:

If you are trying to just face the object at the new heading of ongoingTouchPos, I would say try this.

  transform.LookAt(ongoingTouchPos);