I am having a lot of trouble getting a portion of my script to work correctly.
To set up the script to see precisely what the problem is all you should have to do is provide the same transform for Dragon, Camera Attachment Point, and link the Main Camera to a camera.
// Set variables
var dragon : Transform;
var mainCamera : Transform;
var cameraAttachmentPoint : Transform;
var horizontalAimingSpeed : float = 270;
var verticalAimingSpeed : float = 270;
var maxHorizontalAngle : float = 40;
var minHorizontalAngle : float = -40;
var maxVerticalAngle : float = 30;
var minVerticalAngle : float = -40;
var smoothingTime : float = 5.0;
var cameraOffset : Vector3 = Vector3(0, 1, -4);
var lowAimPosition : Vector3 = Vector3(0, 6, -1);
var highAimPosition : Vector3 = Vector3(0, -1.25, 0.75);
@System.NonSerialized
var lowAimPositionPercentage : Vector3;
@System.NonSerialized
var highAimPositionPercentage : Vector3;
@System.NonSerialized
var angleH : float;
@System.NonSerialized
var angleV : float;
@System.NonSerialized
var aimRotation : Quaternion;
@System.NonSerialized
var aimPosition : Vector3;
@System.NonSerialized
var dragonRotation : Quaternion;
var reticle : Texture;
function Update () {
// Set dragonRotation
dragonRotation = dragon.rotation;
// Make sure input axis dont go beyond -1 or 1
angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * horizontalAimingSpeed * Time.deltaTime;
angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * verticalAimingSpeed * Time.deltaTime;
// Limit angles to min and max
angleH = Mathf.Clamp(angleH, minHorizontalAngle + dragonRotation.eulerAngles.y, maxHorizontalAngle + dragonRotation.eulerAngles.y);
angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
// Determine if angleV is low
if (angleV < 0) {
lowAimPositionPercentage = dragonRotation * (angleV / minVerticalAngle * lowAimPosition);
}
else {
lowAimPositionPercentage = Vector3.zero;
}
// Determine if angleV is high
if (angleV > 0) {
highAimPositionPercentage = dragonRotation * (angleV / maxVerticalAngle * highAimPosition);
}
else {
highAimPositionPercentage = Vector3.zero;
}
// Set aimPosition
aimPosition = (cameraAttachmentPoint.position + (dragonRotation * cameraOffset)) + (lowAimPositionPercentage + highAimPositionPercentage);
// Set aimRotation
aimRotation = Quaternion.Euler(-angleV, angleH, 0);
// Set mainCamera position
mainCamera.position = Vector3.Lerp(mainCamera.position, aimPosition, smoothingTime * Time.deltaTime);
// Set mainCamera rotation
mainCamera.rotation = aimRotation;
}
function OnGUI () {
// Draw the reticle
GUI.DrawTexture(new Rect(Screen.width / 2 -(reticle.width*0.5), Screen.height / 2 -(reticle.height*0.5), reticle.width, reticle.height), reticle);
}
The issue is with the follow portion of the code.
When the dragon transform rotates <> 0/360 the camera pops due to euler angles.
What Im trying to do is limit the cameras rotation to be within -40 and +40 degrees of the dragons rotation.
The script performs as I intend with the exception of the camera ‘pop’.
Here’s the problem area:
// Limit angles to min and max
angleH = Mathf.Clamp(angleH, minHorizontalAngle + dragonRotation.eulerAngles.y, maxHorizontalAngle + dragonRotation.eulerAngles.y);
angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
Really need the help, spent hours on it yesterday and today with no results, very demoralizing ![]()