- Home /
Question by
unity_xwiLJNAO8nUwJg · Mar 26, 2019 at 12:12 AM ·
rotationquaternionrotate objectsincos
Choose starting point/angle for rotation with cos and sin
Hi.
I'm currently working on a project where I need objects to move around a circular object. Everything is working fine except the object always starts at the left of the object (even if I instantiate it elsewhere). I know it has something to do with the cos and sin, probably because I'm not setting a start direction? Here's my code:
float distanceBetweenCenter;
distanceBetweenCenter = AssignedPlanet.GetComponent<CircleCollider2D>().radius * AssignedPlanet.transform.localScale.x;
_timer += Time.deltaTime * _larvaSpeed;
float x = -Mathf.Cos(_timer) * distanceBetweenCenter;
float y = Mathf.Sin(_timer) * distanceBetweenCenter;
Vector3 pos = new Vector3(x, y, transform.position.z);
transform.position = pos + new Vector3(AssignedPlanet.transform.position.x, AssignedPlanet.transform.position.y, transform.position.z);
var dir = AssignedPlanet.transform.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle + 90, Vector3.forward);
The part of the code that snaps my object to the left at start is that one:
float x = -Mathf.Cos(_timer) * distanceBetweenCenter;
float y = Mathf.Sin(_timer) * distanceBetweenCenter;
Vector3 pos = new Vector3(x, y, transform.position.z);
transform.position = pos + new Vector3(AssignedPlanet.transform.position.x, AssignedPlanet.transform.position.y, transform.position.z);
I really could use some help here :) thank you for looking this up!
Comment