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.
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

  • edited
    Why are you normalising your position? That literally is going to destroy any actual positional information in that vector...

    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.
  • Oh! sorry, so I should just explain that the magnitude is irrelevant because Physics2D.gravity should only be affected by the direction, and holds it's own constant magnitude. Besides that, in this case, transform.position is at the origin, so a vector difference is irrelevant.
  • @disleckcia Thanks for your help man - I just found the problem, and boy is it embarrasing
  • Bracula said:
    @disleckcia Thanks for your help man - I just found the problem, and boy is it embarrasing
    Camera looking down onto the x/z plane instead of the x/y plane like your code assumes? ;)
  • Nope...I only checked the position in start, so the value was never changing. Just moved to update and everything is perfect
Sign In or Register to comment.