- Home /
MouseOrbit Camera Lerp
I'm making a game where you control 2 characters, when u press "E", it switch between them. For my camera I'm using unity's MouseOrbit script. When I switch the character been controlled I change the focus of my camera, but it jumps to the new position. How can I make it smoothly "lerp" to that position? Codes would help, I'm not used to java, I usually use C#.
That's the code as it is now:
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
var player1 : Transform;
var player2 : Transform;
var activePlayer : Transform;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
target = player1;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function Update(){
SwitchFocus();
}
function SwitchFocus(){
if (Input.GetKeyDown(KeyCode.E)){
Debug.Log("oi");
if(activePlayer == player2){
activePlayer = player1;
}else{
activePlayer = player2;
}
SetActivePlayer();
}
}
function SetActivePlayer(){
target = activePlayer;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
This isn't java, it's JavaScript (it's a very different thing, see) but I'll let that slide. I could give you a C# translation of this script, if it'd help?
I actually meant JavaScript, and yes it would help, so I can work only with C# scripts. But I still need to find a way to move the camera, or maybe a way to store the 2 positions the camera would have in a variable, so I can lerp between them.
I'm feeling generous.
public Transform target;
public float distance = 10.0f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float y$$anonymous$$inLimit = -20f;
public float y$$anonymous$$axLimit = 80f;
float x = 0.0f;
float var y = 0.0f;
public Transform player1;
public Transform player2;
private Transform currentPlayer;
// I removed the $$anonymous$$enu thing, because it goes outside the class...
void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
target = player1;
// $$anonymous$$ake the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void Update(){
SwitchFocus();
}
void SwitchFocus(){
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)){
Debug.Log("oi");
if(activePlayer == player2){
activePlayer = player1;
}else{
activePlayer = player2;
}
SetActivePlayer();
}
}
void SetActivePlayer(){
target = activePlayer;
}
void LateUpdate () {
if (target) {
x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * Vector3(0.0f, 0.0f, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static float ClampAngle (float angle, float $$anonymous$$, float max) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
}
And now you don't have to use JavaScript at all.
Answer by syclamoth · Mar 15, 2012 at 03:31 AM
How about this: remove any modifications you've done to this script, and go about it from a different angle.
What is it, exactly, that you want to achieve? You want the target of the mouseOrbit to smoothly translate to the position of the new character. So, why modify the orbit script at all?
All you need to do is set the orbit script to follow an empty transform, and move that around in a smooth fashion. After all, the mouse orbit doesn't need to know about how its target is being moved, so splitting the scripts up makes sense.
On this new object, add a script that works something like this:
public Transform initialTarget;
Transform currentTarget;
Transform nextTarget;
void Start()
{
currentTarget = initialTarget;
transform.parent = currentTarget;
}
public void MoveToNewTarget(Transform newTarget, float time)
{
transform.parent = null;
nextTarget = newTarget;
StartCoroutine(MoveCoroutine(time));
}
IEnumerator MoveCoroutine(float timeToMove)
{
float currentTime = 0;
while(currentTime < timeToMove)
{
float currentDistance = currentTime / timeToMove;
transform.position = Vector3.Lerp(currentTarget.position, nextTarget.position, currentDistance)
transform.rotation = Quaternion.Slerp(currentTarget.rotation, nextTarget.rotation, currentDistance);
currentTime += Time.deltaTime;
yield return null;
}
currentTarget = nextTarget;
nextTarget = null;
transform.position = currentTarget.position;
transform.rotation = currentTarget.rotation;
transform.parent = currentTarget;
}
Whenever you want to switch to a new target, just call MovableTarget.MoveToNewTarget(nextTransform, 5);
to move to transform 'nextTranform' ovef 5 seconds.
I'm trying to make it work, will get back with feedback soon.
Edit:
Thanks alot, it worked just as i wanted it to.
Your answer
Follow this Question
Related Questions
Single Camera Lerping between three characters? (code provided) 0 Answers
character switching on collision 1 Answer
Lerping Field Of View is buggy 1 Answer
[QuaternionSlerp] Lookback script 1 Answer
Zooming camera using move z position. 0 Answers