- Home /
Question by
George Thomas · May 04, 2015 at 11:32 AM ·
c#rotationscript.
[C#] Rotate Turret, Only On X and Z Axis
I currently have a turret model in my game, which, through the use of my custom script, rotates to look at the player. However, the script causes the turret to rotate on all axis, when I actually do not want it to look up or down.
Here is the script:
public GameObject player;
public bool friendly;
public float rotationSpeed;
private Transform target;
private Quaternion lookRotation;
private Vector3 direction;
void Start () {
}
void Update () {
if (!friendly) {
target = player.transform;
direction = (target.position - transform.position).normalized;
lookRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
}
}
So, as you can see, it is written in C#. It simply rotates the turret to look at the player. However, it rotates the turret on the X, Y, and Z axis, when I do not want it to rotate on the Y axis.
Any suggestions or help?
Thanks.
Comment
Answer by lll4louis · Apr 20, 2019 at 03:23 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour
{
public int rotateSpeed = 5;
void Update()
{
transform.Rotate(rotateSpeed * Time.deltaTime, 0, 0);
}
}
only on z-axis
For x-axis, change the void Update
void Update()
{
transform.Rotate(rotateSpeed * Time.deltaTime, 0, 0);
}