Using Local Angles to target enemy object
Hey, so ive been working on a turret, the turret sits ontop of the player ship and im wanting to only rotate the object in the z axis, ive gotten so far following all these forums the object will now stay only rotating on the z axis but when i rotate the player on its x or y axis which is the parent for the turret, the turret will then rotate in the incorrect rotation, so say when i have the player ship look directly at the enemy, the turrent will perfectly look at the enemy in the z axis however if i was to flip upside down (in x axis) the turrent will then face directly oposite the enemy.
hope you can understand, here is my code so far:
using UnityEngine;
using System.Collections;
public class MainTurret : MonoBehaviour {
public Transform Enemy;
public Transform playerBody;
public float strength= 0.5f;
// Use this for initialization
void Start () {
playerBody = GameObject.Find ("Player").transform;
Enemy = GameObject.Find ("earthEnemy").transform;
}
// Update is called once per frame
void Update () {
Vector3 turretLookAt = new Vector3(Enemy.position.x,Enemy.position.y, Enemy.position.z);
Quaternion RotationGun = Quaternion.LookRotation(transform.position - turretLookAt);
transform.rotation = Quaternion.Slerp (transform.rotation, RotationGun, Time.deltaTime * 2.0f);
transform.localEulerAngles = new Vector3(Enemy.rotation.x,Enemy.rotation.y, transform.localEulerAngles.z);
}
}
Your answer
Follow this Question
Related Questions
Questions about Rotations in unity 1 Answer
3 Axis Camera Rotation 0 Answers
Camera X Rotation Problems 0 Answers