Vector2.Angle in Unity is returning 90 no matter what - which sucks.
Hello everyone.
So for those of you that were at the Joburg June meetup, and remember my game Physisistance, (focused feedback), I've hit a problem since I started rebuilding in Unity 5. Originally, I had a direction vector found from the centre of the circle to my player object, so that gravity could be directed depending.
This works fine in the rebuild, as far as I can tell, but the problem comes in when I try to use that same vector to find an angle:
I hope that makes sense out of context. Anyway, the value I'm getting for Angle is always 90. I know that _direction is changing if I move the player around, so it seems pretty strange that the angle isn't affected at all. Please, if anyone has any help, or knowledge of possible changes in Unity behaviour between versions, I'd really appreciate it. Thanks.
So for those of you that were at the Joburg June meetup, and remember my game Physisistance, (focused feedback), I've hit a problem since I started rebuilding in Unity 5. Originally, I had a direction vector found from the centre of the circle to my player object, so that gravity could be directed depending.
public Vector3 playerDir; void Update () { playerDir = GameObject.FindWithTag("Player").transform.position.normalized; Physics2D.gravity = playerDir*9.81f; }
This works fine in the rebuild, as far as I can tell, but the problem comes in when I try to use that same vector to find an angle:
void Start() { _circleCentre = GameObject.Find ("CircleCentre"); _direction = new Vector2(_circleCentre.GetComponent<GravityScript>().playerDir.x, _circleCentre.GetComponent<GravityScript>().playerDir.y); } void AdjustAngle() { if(transform.position.x > 0) Angle = 180 - Vector2.Angle(new Vector2(0,1), _direction); else Angle = Vector2.Angle(new Vector2(0,1), _direction) - 180; transform.localEulerAngles = new Vector3 (0,0,Angle); }
I hope that makes sense out of context. Anyway, the value I'm getting for Angle is always 90. I know that _direction is changing if I move the player around, so it seems pretty strange that the angle isn't affected at all. Please, if anyone has any help, or knowledge of possible changes in Unity behaviour between versions, I'd really appreciate it. Thanks.
Comments
To find a relative direction you need to subtract your starting point from the position of an object, so you should probably do something like playerDir = GameObject.FindWithTag("Player").transform.position - transform.position to give you an vector with valuable 3D space information in it.
The magnitude of playerDir is then the distance between the two objects and you're free to normalise playerDir if you don't care about magnitude and only want a relative direction.