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;
}
}
- 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.