rotate by direction

205/5000
how can I rotate an object on the y axis in a certain direction? I have 3 directions and the object instances as soon as they are created I have to turn in that direction and then move on the Z

for (int i = 0; i <= directions.Count-1; i++)
        {
            float angles = 0;
            Vector3 dir = directions[i];
            angles = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
            print(angle);
            Transform b = Instantiate(ball, spawn.transform.position, new Quaternion(0,angles,0,0));
           
            Debug.DrawLine(spawn.position, dir * 30, Color.black);
            balls.Add(b);

        }

Ball code:

  void Update()
    {
        transform.position += transform.forward * Time.deltaTime * 30f;
        //Debug.DrawLine(this.transform.position, this.transform.forward * 20f, Color.green);
       
    }
new Quaternion(0,angles,0,0));

This is not how Quaternions work!
Try this:

Quaternion.Euler(0, angles, 0);
1 Like

Also, to get Y rotation you want to use X and Z not Y and X… i.e. Atan2(dir.x, dir.z) * Mathf.Rad2Deg;

1 Like