- Home /
Float the camera back to his position
I have camera around my player, when I hold down right click it'll rotate around the player. But when I release the right click, I want it to go back to its original position/rotation. To rotate:
if(Input.GetMouseButton(1) == true)
{
rotateSpeed = Input.GetAxis("Mouse X") * speed;
transform.RotateAround(player.transform.position, Vector3.up, rotateSpeed);
}
When the camera isn't rotating anymore, a timer will count down from 3 seconds, after this the camera should float nicely back to the standard position, I thought to reverse the actions but this doesn't work:
public void ReturnCamera()
{
if(timeLeft <= 0)
{
//changed Vector3.up to vector3.down
transform.RotateAround(player.transform.position, Vector3.down, rotateSpeed);
timeLeft = 3;
}
}
Just as a note, you never have to (and never should) write == true
or == false
. An if statement, evaluates the information in the brackets to either true
or false
.
You're basically going either:true == true
orfalse == true
Therefore there is no need for == true
.
There is also never a need for == false
as you can just invert the value using !
(not).
Heres some simple boolean expressions to get you on track :)
if (Input.GetButtonDown("Fire") == true)
//same as
if (Input.GetButtonDown("Fire"))
if (Input.Get$$anonymous$$ouseButton(1) == false)
//same as
if (!Input.Get$$anonymous$$ouseButton(1))
if (timeLeft <= 0)
//same as
if ((timeLeft <= 0) == true)
if (!(timeLeft <= 0))
//same as
if (timeLeft > 0)
The less code (more shorthands), the easier to read, the better the code :)
The first issue is that your Get$$anonymous$$ouseButton(1) code gets executed for all the frames that the button is held down, but your ReturnCamera() code only gets executed for a single frame. That is, on line 7 you do: timeLeft = 3;
. This stops any rotation, so the first time your ReturnCamera() is called, it stops any work done on subsequent calls.
The larger issue is that you don't have any code here that represents 'original position/rotation.' In fact, it is possible that when the mouse button is lifted the rotation is back to the original or past the original one or more times.
In order for someone to offer an accurate solution, they will need to know more about the problem:
Does the object have to rotate back to the original position on the same axes?
Does the object have to rotate back to the original position in the reverse direction of how it got to its position?
Does the object have to reverse the path exactly. For example if the object is rotated two times around, should it do a reverse rotation of two times?
Wow, thanks guys, I'm learning allot here ! And About the floating back; The standard position is right behind the player. It doesn't matter where he rotated to, it should always go back BEHIND the player :)
So if the players position is (0, 0, 0), the standard position of the camera should be (0, 0, -1) and looking in the direction of the player. Thanks !
@robertbu, That's makes so much sence... $$anonymous$$aby I could figure it out now
Answer by robertbu · Jul 14, 2013 at 04:13 PM
Place an empty game object at the same position and rotation of your character.
Make that empty game object a child of the character
Place your camera so that is is look at the back of your character just the want you want it.
Make the camera a child of the empty game object.
Attach this script to the empty game object:
#pragma strict
var speed = 5.0;
var returnSpeed = 90.0;
function LateUpdate() {
Debug.Log(Input.GetAxis("Mouse X"));
if(Input.GetMouseButton(1) == true) {
var rotateSpeed = Input.GetAxis("Mouse X") * speed;
var q = Quaternion.AngleAxis(rotateSpeed, Vector3.up);
transform.localRotation = q * transform.localRotation;
}
else
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.identity, returnSpeed * Time.deltaTime);
}
It uses local rotation, so the empty/game object will always follow the rotation of the character. A local rotation of (0,0,0), which is Quaternion.identity, will place the camera back behind the character.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110049im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object rotates with camera 2 Answers
Return Camera Rotation Z axis to 0 2 Answers
Wonky Camera Behaviours 0 Answers
I am getting a problem with moving and rotating a camera! 1 Answer
Help with camera snap 1 Answer