- Home /
Rotate object permanently around Y-axis while rotating around Z-axis to face mouse?
I'm working on a 2D game and I'm trying to make an object rotate around the Y-axis permanently while having it face the mouse using its Z-axis rotation - like a side view of a fidget spinner on a finger.
float zCam = -Camera.main.transform.position.z;
var mouseVector = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zCam);
var mPos = Camera.main.ScreenToWorldPoint(mouseVector);
var lookPos = mPos - transform.position;
lookPos.z = 0;
float rotationZ = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
This is my current code, but I can't get the object to rotate permanently around the Y-axis using this code! Can anyone help?
Edit: I should mention it's a 2D game with 3D objects - hence the rotation
Answer by kraa · Dec 03, 2017 at 11:28 AM
I solved it by using two objects in a hierarchy. The child rotates around itself while the parent rotates to face my finger.
Your answer
Follow this Question
Related Questions
Random.rotationUniform clarification 4 Answers
Rotate a GameObject in the Vector3 direction? 1 Answer
Rotate children (Cards) on a panel (maybe using lerp, or another method?) 1 Answer
Can this be rewitten to control axis locally instead of globally? 1 Answer
How can I rotate at a constant speed to look at a point? 2 Answers