I don’t know whether this is a stupid question or not because I am pretty good with unity. I am trying to find out how i can smoothly rotate my camera AFTER a button has been pressed.
The game that I am making, starts off with a main menu. On this menu there is a choose character button which the player will press to choose his/her character. After the player has pressed this button, the camera is supposed to rotate clockwise (y axis turn) to face the available characters. I have found codes to rotate the camera, but the rotation occurs immediately after the button is pressed, so it ends up the same as just moving my camera’s position. I will put the code down for my camera rotating instantly and i will accept any advice or help anybody is willing to offer:
var ingamecamera: GameObject;
function OnMouseUp(){
ingamecamera.transform.Rotate(0,90,0);
}
The code above is placed on my button so that when it is pressed the game object of my camera will turn.
var myTransform: Transform; //Object you want to rotate
var target: Transform; //The game object that you want to face
var rotationSpeed: float = 5;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*TimedeltaTime);
This answer is quite late and i realise that, but i did manage to fix it a while a go, but noone had put the answer up here. If for that code above, you just put the last line into a onMouseUp, kind of function it will make the camera face the target whenever the mouse is clicked with a slow or fast rotation speed based on the rotationSpeed variable.
I am not using it with a click anymore and I am actually using it on an enemy(the myTransform is set to an enemy prefab) to move from each waypoint. For those writing a waypoints script and you want to know how to make the enemy move towards the target after it has rotated, heres the script:
var moveSpeed: int = 10; //can be whatever you want
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;