- Home /
Smooth Character Movement
I'm working on a 2D game and I want to know how to move my character smoothly like a ease in ease out type of thing. I though about using lerp but that doesn't work because the start and end vectors would be two global points. Here is what I have so far, how do I make it smooth?
var speed : float;
function Update(){
transform.position.x = 0;
MoveFunc();
}
function MoveFunc(){
var controller : CharacterController = GetComponent(CharacterController);
if(Input.GetButton("left")){
controller.Move(Vector3.forward * -speed * Time.deltaTime);
}
if(Input.GetButton("right")){
controller.Move(Vector3.forward * speed * Time.deltaTime);
}
}
Thanks!
Answer by Joshua · Jul 08, 2011 at 03:17 AM
Use Input.GetAxis for this, and define your own axis under Edit>Project Settings>Input
For instance Input.GetAxis( "Horizontal" ), will, when you press the left key shoot from zero to -1 over a small amount of time, and to +1 if you press the left key. This small amount of time allows you to define
if( Input.GetAxis( "Horizontal" )
transform.Rotate( Vector3.up, rotSpeed * Input.GetAxis( "Horizontal" );
When you press left or right, after about half a second GetAxis will return -1/1, so the object will rotate at full 'rotSpeed' around it's y axis. In the first half second it will rotate slower, because (for instance) GetAxis will return 0.5 after 0.1 seconds of pressing right, so it'll rotate to the right at half spead.
Setting up an axis for your needs takes a lot of tweaking, but it's the best way to get smooth easing in and out of movement.
Your answer
Follow this Question
Related Questions
Undesirable character flipping 0 Answers
adjust the player in the ground And movements 2 Answers
Another Double Jump Question 0 Answers
making an object move to a certain point 3 Answers
GetKey and GetKeyDown are always registered? [Answered] 2 Answers