- Home /
Other
make turret point where mouse points
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.
Answer by aldonaletto · Jun 29, 2012 at 03:39 PM
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 ShooterAdd 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 } }
Well that code does'nt work properly, the turret itself goes into the car and is all messed up when looking on a object besides or behind the turret.. I use a gameobject to rotate 2 parts at a time as one and to keep the rotations, but the center of rotation thingy is sometimes not in the center.
Hmm, doesnt that just make a plane and aim at the plane? Is it gonna be accurate if I shoot at a longer distance? And I want it to have a wide field of view, preferably if there is a way to have like either many planes all around the car or any other way so I can shoot around the car.
The plane created has nothing to do with the Unity plane primitive: it's an infinite logical plane, created at the empty object position and parallel to the ground. Nothing is drawn, since it's a purely logical entity - but you can detect raycasts with the appropriate Plane.Raycast function. Better yet, only the plane detects these raycast hits, thus the other objects in scene are completely ignored.
Answer by multinfs · Jul 05, 2012 at 12:48 PM
@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.
Answer by multinfs · Jul 05, 2012 at 01:04 PM
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
Removing the answer status from this as this is not an answer. Closing question per your desire.
Follow this Question
Related Questions
Rotating object towards mouse point 1 Answer
Rotating a turret body and head 1 Answer
Look around when holding down mousebutton 1 Answer
Camera rotate with mouse in TPC 0 Answers