- Home /
smooth rotation / movement ?
i am making a 2D game that has 3d rotation with camera i can make it rotate and change axis but i want to make it smoothly transition how can i make smooth rotation ? and smooth movement ? this is how i do it :- transform.Rotate(0,-90,0); i want it to be smooth and in movement there is Distance from player i change the distance from X 10 to X = 0 and Z 0 to Z = 10 but all happen instantly how can i fix this ?
Answer by aldonaletto · Oct 08, 2011 at 11:28 AM
Smooth in Unity means Lerp! (Slerp, for Quaternions) You should use the "Lerp filter", a smart use of Lerp that behaves like a smoothing filter. Attach this script to the camera:
var speed: float = 2.5; var cameraAngles: Vector3; // the target angles around x, y and z var cameraPos: Vector3; // the target position
function Start(){ cameraAngles = transform.eulerAngles; cameraPos = transform.position; }
function Update(){
transform.position = Vector3.Lerp(transform.position, cameraPos, speed*Time.deltaTime);
var newRot = Quaternion.Euler(cameraAngles); // get the equivalent quaternion
transform.rotation = Quaternion.Slerp(transform.rotation, newRot, speed*Time.deltaTime);
} You just set the angle and position you want in cameraAngles and cameraPos, and the camera goes smoothly to the new rotation/position.
I know this is an old post; however, I just came across it when I was searching for lerping examples and I wanted to comment on it because I think it may be incorrect. I am new to unity but after reading another site, I don't think you can just call Vector3.Lerp()
like you are doing in the example below:
transform.position = Vector3.Lerp(transform.position, cameraPos, speed*Time.deltaTime);
var newRot = Quaternion.Euler(cameraAngles); // get the equivalent quaternion
transform.rotation = Quaternion.Slerp(transform.rotation, newRot, speed*Time.deltaTime);
I believe you need to store the original position of the object and the end position when you begin lerping so you can use it in each FixedUpdate()
call. Every time FixedUpdate()
is called, transform.position
will be different during an interpolation and unless I am missing something, speed*Time.deltaTime
is incorrect too. Vector3.Lerp()
only interpolates the value given in the third parameter between two points. It is almost like a "percentage" of how far the lerp is supposed to move between the start/end points. Lerping will span across several FixedUpdate()
calls. You can't just magically call Vector3.Lerp()
, give it the three parameters and forget about it.
Lerping should begin in Update()
and be handled in FixedUpdate()
to ensure consistency across a variable frame rate.
Same lerping concepts apply for both movement and rotations.
See below for a better example/explanation of what I mean:
http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly
$$anonymous$$ate, check out this guy's $$anonymous$$arma (Reputation). I think he has a good idea ;)
You can magically call Lerp and forget about it if you have placed it in Update, which happens to be a Loop. And the nature of Variables mean that the information is Updated frame on frame.
There is an unusual but very useful application of Lerp referred to as the Easing Lerp that moves the object along by a FIXED percentage (hence the speed*deltaTime) between itself and some point. The effect is that you will achieve smooth movement between those two ever updating points and the Lerp never runs out.
Super cool, eh?
Answer by torsten · Oct 08, 2011 at 12:13 PM
Hi,
I don't exactly understand what you mean, but have a look at Quaternion.Slerp() for smooth interpolation of rotation and for translation Vector3.Lerp().
Torsten
Your answer
Follow this Question
Related Questions
Can't force my circle sprite to move/rotate smoothly. Code is very simple. 0 Answers
Smooth rotation... 1 Answer
Smooth Attached Object movement 1 Answer
Undesirable character flipping 0 Answers
in 2d game, while draging and dropping object its movement very slow , it can't move smoothly ? 0 Answers