# Rotate turret on vehicle

I’m making simple turret which rotates towards mouse position. Simple as hell … yyy not quite.

http://unity.alfa.p-c.pl/cars/

``````public class gun : MonoBehaviour {
public Transform body;

private float lastAngle;

void Update () {

Plane playerPlane = new Plane(new Vector3(0,1,0), transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 10.0f;
if (playerPlane.Raycast (ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
float angle = Quaternion.Angle( targetRotation, transform.rotation );
transform.RotateAround(body.transform.position, body.transform.up,angle*0.1f);
}
}
}
``````

Thera are two methods of doing this:

1. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, time.deltaTime()) - good solution but problem is when vehicle turns over because turret is tring always to go straight to the mouse point so it looks like the turret is separating from body.
2. transform.RotateAround - this is great because no matter what is going on with vehicle the turret is always in place and turning only in Y-axis looking from body of vehicle, But !!!. Angle from Quaternion.Angle has only positive values from 0 -179 and 179-0. So in this demo I can only turn right with turret because angle has positive values, turning left causes that the turret goes … right with angle
Now … I have no idea how to jump this problem.

Any help ?

Have you tried Transform.RotateTowards() ? I think it’ll work for you here.

RotateTowards() works the same as Quaternion.Slerp … unless I’m doing something wrong, it’s rotating toward mouse position when vehicle turns over, turret separates from body of vehicle
RotateAround is the best solution because it gets point of rotation and vector3 of through point. So no matter what is going on with vehicle ( can fly in every directions and rotations ) turret is always in same position in relation to body. But this angle … I have an euler angle but only positive values.

Found solution. Maybe somebody will search for something like this

``````void Update () {

Plane playerPlane = new Plane(new Vector3(0,1,0), transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 10.0f;
if (playerPlane.Raycast (ray, out hitdist))
{
targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
float angle = Quaternion.Angle( targetRotation, transform.rotation );

if (Vector3.Dot(transform.TransformDirection(Vector3.right), (targetPoint - transform.position)) < 0)
{
// left
transform.RotateAround(body.transform.position, body.transform.up, angle * (-1) * Time.deltaTime * 5f);
}
else
{
// right
transform.RotateAround(body.transform.position, body.transform.up, angle * Time.deltaTime * 5f);
}
}
}
``````

The clue is to fine left/right vector direction which can be done witch Vector3.Dot();

MAN, Y O U S A V E M Y L I F E !!! Thank you!!!