- Home /
Can the Camera Smoothly rotate AFTER a button is pressed? (Javascript)
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.
You want to use Lerp or Slerp to move it smoothly. This has an example of smoothly moving an object.
http://unitygems.com ... "beginner" article ... section on timers and Invoke()
just the man I was looking for! .. @whydo ..
if you look at your Invoke()/etc section in the beginner/ meisterwerk.
notice that the example is indeed somewhat advanced ....... using invoke(s), from update, locks, and so on. looking at the OPs question here for example (and there have been about 3 others the same, incredibly)
I was thinking, maybe there should be just in a few lines "simplest example" of invoke, prior to your advanced example???
so literally how to put an "invoke" in start and "have it do something in a few seconds". possibly your immediate large example is too advanced? Food for thought. You the man.
I admire the OP here's consistent, clear code and thought processes. But nobody's ever told the bloke how to make a timer!
Agreed and done. Also (sorry) added one example of where a coroutine would be useful for this kind of thing ;)
@$$anonymous$$oshX87 in that link both @Fattie and I posted I've added an example of rotating an object over time.
Answer by KoshX87 · Jan 04, 2013 at 08:32 PM
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;