- Home /
How to rotate transform only on its Y axis?
Hi,
I'm trying to rotate an object only on its Y-axis in order to look at my player all the time, (like a turret motion)
I have tried this code:
var step = speed * Time.deltaTime;
Vector3 targetDir = target.position - transform.position;
Debug.DrawRay(transform.position, targetDir, Color.red);
Model.rotation = Quaternion.LookRotation(targetDir, Vector3.up);
and this one:
var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
But my object still rotates on all axis as seen on this image:
Any idea on how I can achieve this behaviour?
Answer by Shameness · Jul 04, 2018 at 09:03 AM
Vector3 initialForward;
void Start(){
initialForward = transform.forward; // change this if its obj looking wrong way
}
void Update(){
Vector3 lookPos = target.position - transform.position;
Vector3 forward = (transform.position + initialForward) - transform.position;
lookPos.y = 0;
transform.eulerAngles = new Vector3(0, Vector3.SignedAngle(lookPos, forward, Vector3.down));
Debug.DrawRay(transform.position, lookPos, Color.red);
Debug.DrawRay(transform.position, forward, Color.green);
}
Just tested.
Edit: added screenshot
$$anonymous$$y full script.
public class LookAtScript : $$anonymous$$onoBehaviour {
public GameObject targetObj;
public float speed;
Transform target;
Vector3 initialForward;
// Use this for initialization
void Start () {
initialForward = transform.forward;
target = targetObj.transform;
}
// Update is called once per frame
void Update () {
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
if (Input.GetAxisRaw("Horizontal") > 0f || Input.GetAxisRaw("Horizontal") < 0f)
{
transform.position += new Vector3(h, 0) * speed * Time.deltaTime;
}
else if (Input.GetAxisRaw("Vertical") > 0f || Input.GetAxisRaw("Vertical") < 0f)
{
transform.position += new Vector3(0, 0, v) * speed * Time.deltaTime;
}
Vector3 lookPos = target.position - transform.position;
Vector3 forward = (transform.position + initialForward) - transform.position;
lookPos.y = 0;
transform.eulerAngles = new Vector3(0, Vector3.SignedAngle(lookPos, forward, Vector3.down));
Debug.DrawRay(transform.position, lookPos, Color.red);
Debug.DrawRay(transform.position, forward, Color.green);
}
}
Is there any difference if my GameObject is a child Object? Because If I use the script on the parent GameObject (in this case Troll) the script works it locks the rotation on Y, but the Sprite Object which is the one I want to lock its rotation on the Y-axis continues to rotate in all axis.
Your answer
Follow this Question
Related Questions
Having trouble with Position X location on objects 0 Answers
Commands affecting rotation not working 1 Answer
How to get Angle float of specific axis.(Turret clamping related). 2 Answers
My Obstacle Rotations Are Not Correct 0 Answers
Why transform.eulerAngles and rotation in editor transform are different? 0 Answers