- Home /
Question by
Aqurom · Mar 12, 2020 at 10:34 PM ·
c#quaternionrotate objectrotation axischild object
Child of a gameobject lookAt only around Y-axys
I have 3d scene with 3 objects (player, main object and child of main object), I need to make both objects always lookAt the player, by rotate them only around Y-axys... im total beginner in unity and hope there is no abvious solution :)
on main object i have script and this work fine, but when i attach same script to child its looks weird...
void Start()
{
cameraPlayer = GameObject.FindGameObjectWithTag("Player Camera");
}
void Update()
{
Vector3 directionToPlayer = cameraPlayer.transform.position - transform.position;
Quaternion rotateAt = Quaternion.LookRotation(directionToPlayer);
Vector3 rotation = rotateAt.eulerAngles;
platformCanvasRotator.rotation = Quaternion.Euler(0f, rotation.y, 0f);
}
}
снимок.png
(35.9 kB)
Comment
Best Answer
Answer by Namey5 · Mar 12, 2020 at 11:57 PM
Try the following;
void Update()
{
Vector3 directionToPlayer = cameraPlayer.transform.position - transform.position;
directionToPlayer.y = 0f;
transform.rotation = Quaternion.LookRotation (directionToPlayer.normalized);
}
What about it doesn't work? Can you provide more information/images.
I remake prefab a bit, and now its work currectly. Thanks for your help.
Your answer
Follow this Question
Related Questions
issue after 360 degrees rotation 1 Answer
Multiple Cars not working 1 Answer
Odd rotation on the wrong axis 0 Answers
Distribute terrain in zones 3 Answers