I am currently working on a turret that is controlled via a 3rd person view using a cross-hair in the center of my screen to shoot a ray to where the turret should be aimed at. The turret is broken down into 2 parts, the base and the weapons. The base is the parent to the weapons (i.e the weapons are under the category base in the hierarchy view).
Now what my desired result is to have a script that rotates the base along the Y axis only (The weapons should move as well as they are part of the base) and then the weapons (there are 2 which are mounted on each side of the base) should rotate up and down, along the X axis. This is what I have achieved so far.
var hit : RaycastHit;
var turret : GameObject;
var turretWeapon : GameObject;
var turretRotationSpeed = 4.0;
var range = 100.0;
var crosshair: Texture2D;
private var imageCrossHair : Rect;
private var targetRotation : Quaternion;
function OnGUI ()
{
// Draw the cross-hair in the center of the screen
imageCrossHair = Rect((Screen.width - crosshair.width)/2, (Screen.height - crosshair.height)/2 , crosshair.width, crosshair.height);
GUI.DrawTexture (imageCrossHair, crosshair);
Screen.showCursor = false;
}
function Update ()
{
// Shooting the ray from the center of the screen towards where the player is looking
var ray = camera.ViewportPointToRay(Vector3(0.5,0.5,0));
Debug.DrawRay (ray.origin, ray.direction * range, Color.green);
var hit : RaycastHit;
// Getting the coordinates of the turret position
var targetLocation = turret.transform.position;
// Making the rotation of the turret not effect the Y axis
targetLocation.y = 0;
// If the ray hits a plane within the rays range
if (Physics.Raycast (ray, hit)) {
// Getting the hit location of the ray
var hitPoint = hit.point;
// *** Rotating the Turret only around the Y axis *** //
hitPoint.y = 0;
// Determine the target rotation. This is the rotation if the transform looks at the target point.
targetRotation = Quaternion.LookRotation(hitPoint - targetLocation);
// Smoothly rotate towards the target point.
turret.transform.rotation = Quaternion.Lerp(turret.transform.rotation, targetRotation, turretRotationSpeed * Time.deltaTime);
// If the ray didn't hit a plane within the rays range
} else {
// Getting the location of the ray at max range
var targetPoint = ray.GetPoint(range);
// *** Rotating the Turret only around the Y axis *** //
targetPoint.y = 0;
// Determine the target rotation. This is the rotation if the transform looks at the target point.
targetRotation = Quaternion.LookRotation(targetPoint - targetLocation);
// Smoothly rotate towards the target point.
turret.transform.rotation = Quaternion.Lerp(turret.transform.rotation, targetRotation, turretRotationSpeed * Time.deltaTime);
}
}
In the code above I have managed to get the turret to rotate around the Y axis successfully, but every time I try to get the weapons to rotate around the X axis the weapons detach from the base and stick facing towards the Z axis in the world view. (that is with using Quaternion.Lerp and setting hitPoint.x = 0 and targetLocation.x = 0)(And I did re-get the coordinates for hitPoint and targetLocation and didnt use the one that I set their y’s to 0)
I had some success with transform.Rotate(Vector3.right, angle) but i couldn’t get it to only look at the hitPoint, it would follow the base perfectly but just continue spiraling around and around. I am at a loss at the moment on how I would achieve this.
On a side note I noticed that using Quaternion.Lerp slows down the rotation towards the end of a rotation, which is not what I desired for 3rd person aiming. I was looking for something that would rotate something at a constant speed (that I could set) which would more suit 3rd person aiming.
Sorry for the long post, tried to get the problem explained as much as possible, I code in C++ and Java but never coded in Javascript before I started with Unity, so if my coding isn’t up to scratch please tell me
Thanks in advance