I need help with making a script that rotates a turret to where the mouse points,I have made some kind of script but its just messy when turning.
I made it so there is a ray going from the camera to where the mouse points and a long distance and from the hit.point then make the turret LookAt.
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 200000, Color.yellow);
if(Physics.Raycast(ray,hit)){
transform.LookAt(hit.point,transform.up);
}
It works when looking far ahead but when looking like behind the car it is sitting on it just turns weirdly and goes into the car.
EDIT:
I still can’t make this work, I tried with adding Mathf.Clamp to limit rotation of the turret but it was’nt so successful.And I made a empty GameObject that is placed at hit.point to get the coordinates from that point.
var dist: float;
var hitpoint : GameObject;
function Update () {
transform.rotation.z = 0;
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 500000, Color.yellow);
if(Physics.Raycast(ray,hit)){
var rot2 : Vector3 = Vector3(hit.point.x + 90,hit.point.y - 90,0);
dist = Mathf.Abs(Vector3.Distance(transform.position,hit.point));
hitpoint.transform.position = ray.GetPoint(dist);
transform.LookAt(hitpoint.transform.position,transform.up);
transform.rotation.y = Mathf.Clamp(transform.rotation.y, -90,90);
transform.rotation.x = Mathf.Clamp(transform.rotation.x, -20,10);
}
Link to test and see how it behaves HERE

Oh sorry forgot, The box to the left is the right one, the thing to the right was the first try but I made a simple turret later, so the box to the left is the turret with this script.
This script is correct, but the turret will rotate its whole body in the clicked point direction. If you want to make it rotate only around the Y axis, an auxiliary logic plane may be necessary - take a look at this question, where this solution is used.
EDITED: Well, the part you have showed at first was right, but its sequel isn’t - you can’t modify quaternion components directly.
I suppose the capsule is the shooter, and that cubic thing is the turret - am I right? If so, you should create an empty game object, child it to the car and move it to the shooter position, then child the turret and the shooter to this object. Adjust the turret position/orientation so that it’s in front of the shooter and pointing in the empty object’s forward direction (the blue axis).
The hierarchy of these objects will be:
Car Body
EmptyObject <- the script is attached to this object
Turret Model
Shooter
Add the script below to the EmptyObject:
function Update () {
// create a logical horizontal plane at this object's position:
var hPlane: Plane = new Plane(transform.position, Vector3.up);
// create a ray from the mouse pointer:
var ray: Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var dist: float = 0; // this will receive the hit point distance
if (hPlane.Raycast(ray, dist)){ // if the ray hits the plane...
var hitPt = ray.GetPoint(dist); // get the hit point...
transform.LookAt(hitPt); // and look at it
}
}
@aldonaletto It kinda works but it behaves weirdly when moving.I noticed that u made the plane at the position of the turret, which makes it very high of the ground. Now, I made a new vector3 at the turrets position but made it close to the ground. And when I made a cube appear where the mouse is pointing at it goes below ground when looking forward and on top of the ground when looking behind.
Closed this cause it is getting filled, And I can’t comment or edit my posts! I can edit on internet explorer but not on chrome, Got to be a bug.
new post: here