- Home /
Freeze Camera Rotation that is attached to the object in certain moments
Ok so this is the code I have so far:
//public variables
public var Target : Transform; //object to follow
public var SwivelSpeed : float = 1.0;
public var Boundary : int = 70; // distance from edge scrolling starts
public var speed : int = 5;
//private variables
private var theScreenWidth : int;
private var theScreenHeight : int;
// start function
function Start(){
Screen.showCursor = false;
theScreenWidth = Screen.width;
}
// update function
function Update ()
{
RotateTurret();
}
// rotate function
function RotateTurret(){
if (Input.mousePosition.x > theScreenWidth - Boundary)
{
if(Target)
{
localTarget= transform.InverseTransformDirection(Target.transform.position - transform.position);
targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
transform.Rotate(Vector3.up, adjustedAngle);
}
}
else if (Input.mousePosition.x < 0 + Boundary)
{
if(Target)
{
localTarget= transform.InverseTransformDirection(Target.transform.position - transform.position);
targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
transform.Rotate(Vector3.up, adjustedAngle);
}
}
else
{
//// ? freeze camera
if(Target)
{
localTarget= transform.InverseTransformDirection(Target.transform.position - transform.position);
targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
transform.Rotate(Vector3.up, adjustedAngle);
}
}
}
It rotates a turret to look at an objects location. All is good when camera is not attached to the turret. But when I attach the camera to the turret ( because I need the player to be looking where the turret is pointing at .. ) It spins continuosly because the objects is raycasted with the mouse on screen. And the turret spins towards the object , but because the camera is on the turret and the turret spins, the object is never in the same place thus the turret never stops spinning.
So I made a condition to rotate the turret only when on the edges of the screen, but I need to put something in that "else statement" that lets the turret follow the object but the camera freezes or detaches itself from the turret rotation somehow.
Other solutions I've tried so far: Two cameras, on the body and on the turret and disabling them on certain movements but there it's not fluid as it jumps from one camera to the other, comparing mouse positions to previous one but that didn't work either, detaching the camera from the turret and making it follow it with a script, but I can't seem to write a corect script that would rotate around the turret at the same pace with it and around it so I abandoned that ideea.
Basically I need the camera to stop following the turret in that "else" but I stiil need to use this if(Target) { localTarget= transform.InverseTransformDirection(Target.transform.position - transform.position); targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg; adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed); transform.Rotate(Vector3.up, adjustedAngle); }
to rotate my turret.
Can you please help ? I'm really amazed there is not a freeze that can oveeride a camera rotating with it's parent.
Your answer
Follow this Question
Related Questions
Rotation always changing values! 1 Answer
How to make an object rotate on the y axis towards the mouse? 3 Answers
Mouse Orbit snapping issues 0 Answers
Screen.lockCursor messes my rotation 1 Answer