- Home /
Character Movement
Hey, I'm making a 2d game and my character is a sphere. How can I make so that it takes like .5 seconds of holding A or D to make my character get to top speed? Right now it goes top speed the instant I press A or D. Also how can I make the character keep on moving the direction its going even if A or D is let go? Could I get some sample code? Thanks.
Answer by BinaryCaveman · May 23, 2011 at 02:01 AM
There could be several ways, depending on how your character is set up.
If you have a Rigidbody
component attached to the character GameObject
, you could use
public var speed : float = 5;
function Update()
{
rigidbody.AddForce(Vector3(
Input.GetAxis("Horizontal") * speed,
Input.GetAxis("Vertical") * speed,
0));
}
Read more about Rigidbody.AddForce()
here.
You could also have a velocity vector which is updated as a key is pressed, like in this simple example (JavaScript):
public var friction : float = 0.2;
public var speed : float = 0.1;
private var velocity = Vector3(0, 0, 0);
function Update()
{
velocity.x += Input.GetAxis("Horizontal") * speed;
velocity.y += Input.GetAxis("Vertical") * speed;
transform.position += velocity;
velocity *= friction;
}
They both do basically the same thing, except one uses the built-in physics engine, and the other uses your own.
As for keeping the character moving in a direction for a longer time, you can increase the speed
variable or decrease the Rigidbody
's mass in the Rigidbody
example, and reduce the friction
or speed
variable in the second example.
I hope this answers your question!
Additional code (see comments below):
public var collisionParticles : ParticleEmitter;
public var wall : Collider;
function OnTriggerEnter(Collider other)
{
if (other == wall)
{
// instantiate a particle system in the current location with no rotation
Instantiate(collisionParticles, transform.position, Quaternion.identity);
// destroy this GameObject
Destroy(gameObject);
}
}
@script RequireComponent(Rigidbody)
Thanks for the info but my character runs on a character controller not a rigid body. Is there anyway to do this with a character controller?
I see. $$anonymous$$y script still works in that case, but you could probably just attach a FPSInput Controller
to your Character Controller
(which will, in turn, add a Character $$anonymous$$otor
, also). That would probably be a better solution.
Alright thanks, I got it to accelerate but can I adjust it so it's for a 2d game? I really just want to move sideways and not in the z direction. Thank you for all you help so far.
Also to let you get an idea of my game, it's very much like Red Ball, you can play it here http://www.king.com/games/sponsored-games/red-ball/ Thanks.
I have updated my code above with the y
axis ins$$anonymous$$d of the z
. I moves the Input.GetAxis("Vertical") * speed
in the Rigidbody
example to the y
value of the Vector3
, and changed all of the z
's to y
's in the second example.
Answer by Owen-Reynolds · May 30, 2011 at 05:37 PM
There is a window where you can change the arrow keys "speed up" and "coast" rates. Select Edit->ProjectSettings->Input. The Inspector will change. Pop open Axes and then Vertical. Sesitivity is how fast it speeds up, and gravity is coast speed.
Setting Sensitve/Grav to 0.2 will give 5 secs of holding UP arrow to reach full speed, and 5 secs to coast to 0. The range of "Vertical" is from -1 (backwards) to 1 (forwards.) So, the current value of 3 means it adds 3/second, getting to full speed in 1/3 of a second.
I've never tried it, but a Gravity of 0 (or really small) should make it coast forever.
Any script which uses Input.GetAxis("Vertical") uses these settings. Snap is also fun to look at, while the menu is open (it makes you have to slow down before changing directions.)