Answers for "2D Rotate towards GameObject acting odd."
http://answers.unity.com/questions/861007/2d-rotate-towards-gameobject-acting-odd.html
The latest answers for the question "2D Rotate towards GameObject acting odd."Answer by nu-assets
http://answers.unity.com/answers/861803/view.html
You should perform your calculations in local space not in world space.
You can rewrite your Rotate() method like this:
void Rotate()
{
Vector2 dir = transform.InverseTransformPoint(player.transform.position);
float angle = Vector2.Angle(Vector2.right, dir);
angle = dir.y < 0 ? -angle : angle;
transform.Rotate(Vector3.forward, rotateSpeed * Time.deltaTime * angle);
}
This way you have a very simple but solid implementation of a follow script.
To prevent the follower to increase its angular velocity with the size of the angle you could
do the following:
const float Threshold = 1e-3f;
void Rotate()
{
Vector2 dir = transform.InverseTransformPoint(player.transform.position);
float angle = Vector2.Angle(Vector2.right, dir);
angle = dir.y < 0 ? -angle : angle;
if (Mathf.Abs(angle) > Threshold)
{
transform.Rotate(Vector3.forward, rotateSpeed * Time.deltaTime * Mathf.Sign(angle));
}
}Wed, 24 Dec 2014 14:00:16 GMTnu-assetsAnswer by Scribe
http://answers.unity.com/answers/861788/view.html
Okay, so this is a little 'hacky' in that I create a new parent so that I can separate the rotation around z and the local rotation around x (to make the plane upright). There would be a way of doing this without the extra parent, but I should be wrapping presents and not spending hours on Quaternion rotations! :D
The following should be attached to the highest object in your plane object hierarchy:
public float forwardSpeed = 5;
public float turnSpeed = 90;
public float twistSpeed = 180;
public Transform target;
Vector3 targetPos;
Transform this_transform;
Vector3 dir;
Transform parentT;
GameObject parentGO;
void Start(){
this_transform = transform;
parentGO = new GameObject("rotationControl");
parentT = parentGO.transform;
parentT.position = this_transform.position;
this_transform.parent = parentT;
}
void FixedUpdate(){
dir = new Vector3(target.position.x - parentT.position.x, target.position.y - parentT.position.y, 0);
//Look rotation (around Z axis)
Quaternion q = Quaternion.FromToRotation(Vector3.right, dir);
parentT.rotation = Quaternion.RotateTowards(parentT.rotation, q, turnSpeed*Time.deltaTime);
//Twist rotation (around local X axis)
Quaternion q2 = Quaternion.identity;
if(Mathf.Sign(dir.x) < 0){
q2 = Quaternion.Euler(180, 0, 0);
}
this_transform.localRotation = Quaternion.RotateTowards(this_transform.localRotation, q2, twistSpeed*Time.deltaTime);
//Movement (you can use your forces here, this was mainly for testing)
parentT.position += parentT.right*Time.deltaTime*forwardSpeed;
}
This should turn the plane to face the target over time and upright the plane when it becomes upside-down, hopefully without causing any movement in the z axis (you may want to clamp the z value just to be safe)!
Merry Christmas/Happy Holidays!
ScribeWed, 24 Dec 2014 13:27:09 GMTScribe