How rotate turret and follow body object.

I want rotate my turret, but does not work.

This function called once per frame.
And this 2 codes have same problem.

When i use Transform.rotation,turret don’t follow my body.(sphere is testObject)
21951-01.jpg

And if i use Transform.localRotation then drive and turn my car, look like this.
21952-2.jpg

How fix this ? Please help ! and apologise my terrible English.

	void RotateTurret()
	{
		//turret
		Ray ray = _mainCameraTransform.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		if( _terrainCollider.Raycast(ray,out hit,Mathf.Infinity) )
		{
			Vector3 targetPosition = hit.point;
	
//code1
//			Vector3 newRotation = Quaternion.LookRotation(targetPosition - _turretTransform.position).eulerAngles;
//			Vector3 angles = _turretTransform.rotation.eulerAngles;
//			_turretTransform.rotation = Quaternion.Euler(0,Mathf.SmoothDampAngle(angles.y,newRotation.y,ref _ref,1.0f, 40.0f),0);

//code2
//			Vector3 newRotation = Quaternion.LookRotation(targetPosition - _turretTransform.position).eulerAngles;
//			newRotation.x = 0; newRotation.z=0;
//			_turretTransform.rotation= Quaternion.Slerp(_turretTransform.rotation,Quaternion.Euler(newRotation),Time.deltaTime);

			//debug
			testObject.position = targetPosition;
		}
	}

Like 1elfdragon1 said , I am not sure too what you want exactly, but I think you want to ask your turret to look where your cursor is right?

I am assuming that your tank is rolling on a flat ground.

try this (I am writing the ‘if’ condition part only):


if(_your condition_)
{
   Vector3 targetPosition = hit.point;
   Vector3 newRotation = Quaternion.LookRotation(targetPosition - _turretTransform.position,_turretTransform.up).eulerAngles;
   _turretTransform.rotation= Quaternion.Slerp(_turretTransform.rotation,Quaternion.Euler(newRotation),Time.deltaTime);
}

Turret rotation for a tank that can move over a terrain is complicated. Below is a script I wrote for another question, but the code was buried down some levels in comments, so I’ll repost it here. It has a restriction on the angle of the gun. This restriction is based on the starting angle of the gun.

This code is designed to be added to the turret. Then the guns (as child objects) would initialize the ‘gun’ transform.

I don’t know how you are handling your target, but in this script ‘target’ needs to be initialized.

#pragma strict

var target : Transform;
var gun : Transform;
var turretDegreesPerSecond : float = 45.0;
var gunDegreesPerSecond : float = 45.0;

var maxGunAngle = 45.0;

private var qTurret : Quaternion;
private var qGun : Quaternion;
private var qGunStart : Quaternion;
private var trans : Transform;

function Start() {
    trans = transform; 
    qGunStart = gun.transform.localRotation;
}

function Update () {
    var distanceToPlane = Vector3.Dot(trans.up, target.position - trans.position);
    var planePoint = target.position - trans.up * distanceToPlane;

    qTurret = Quaternion.LookRotation(planePoint - trans.position, transform.up);
    transform.rotation = Quaternion.RotateTowards(transform.rotation, qTurret, turretDegreesPerSecond * Time.deltaTime);

    var v3 = Vector3(0.0, distanceToPlane, (planePoint - transform.position).magnitude);
    qGun = Quaternion.LookRotation(v3);

    if (Quaternion.Angle(qGunStart, qGun) <= maxGunAngle)
       gun.localRotation = Quaternion.RotateTowards(gun.localRotation, qGun, gunDegreesPerSecond * Time.deltaTime);
    else
       Debug.Log("Target beyond gun range");
}