- Home /
Align camera and turret aim
Hi
I have this tank game im working on but I have a little problem, I need to align the turret rotation with the camera so the tank shoots where the camera is pointing but only on the Y axis (Camera's X axis), I tried to do this by changing the turret's rotation but you cant change just the rotation of one axis without affecting the other two axes, even if you add turret's rotation back in the other two axes it doesnt look right...
Any idea how can I do this properly?
Here is the Camera code Im using:
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour {
public GameObject Player = null;
public Vector3 OffSet;
public int CameraIndex = 0;
private OnlineTankController _Controller;
float distance = 10f;
float xSpeed = 40f;
float ySpeed = 120f;
public float yMinLimit = -20.0f;
public float yMaxLimit = 80.0f;
private float x = 0f;
private float y = 0f;
// Use this for initialization
void Awake(){
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
if (GetComponent<Rigidbody>() != null){
rigidbody.freezeRotation = true;
}
}
void Start() {
_Controller = Player.GetComponent<OnlineTankController>();
transform.rotation = _Controller.transform.rotation;
}
// Update is called once per frame
void LateUpdate () {
switch (CameraIndex){
case 0:
distance = 10;
break;
case 1:
distance = 15;
break;
case 2:
distance = 20;
break;
}
if (Input.GetButtonDown("Camera")) {
CameraIndex += 1;
}
if (CameraIndex >= 3){
CameraIndex = 0;
}
if (Player != null){
x += Input.GetAxis("TurretX") * xSpeed * 0.02f;
y -= (float)(-Input.GetAxis("TurretY") * ySpeed * 0.02f);
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Quaternion Turretion = Quaternion.Euler(Player.transform.rotation.eulerAngles.x, x, Player.transform.rotation.eulerAngles.z);
Vector3 position = rotation * new Vector3(0.0f, 2, -distance) + Player.transform.position;
transform.rotation = rotation;
//_Controller.Turret.transform.Rotate(0,Input.GetAxis("TurretX")*10,0);
_Controller.Turret.transform.rotation = Turretion;
transform.position = position;
}
}
static float ClampAngle(float angle, float min,float max){
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
$$anonymous$$y tank setup is a little different then yours because I use the view of the camera through a remote firing system and my camera moves as the barrel elevates and it sounds like you just want the turret rotation part. Is there a reason why you can't attach the camera to the turret? If the camera was a child of the turret and then you align the initial view, when you rotate the turret transform you wouldn't have to any math camera would just rotate with the turret.
Your answer
Follow this Question
Related Questions
Freeze Camera Rotation that is attached to the object in certain moments 0 Answers
How to freeze the rotation or the position on the camera transform? 1 Answer
how do i make first person character rotate left and right along with camera? 0 Answers
Override Oculus Orientation 5.3 1 Answer
FPS camera movement breaks GameObject 2 Answers