- Home /
Character constant movement 2D...
I am very new to Unity and am trying to create a 2.5D sidescroller with only two interactions. For E.g, "Jump and Slide".
Therefore how do I script my character to constantly run/move so the player can concentrate on the obstacles ahead?
Thanks :)
Answer by Rabwin · Apr 24, 2011 at 01:28 PM
Wow it's been a long time since you asked and probably figured it out already, but I'll go ahead and answer since I need to clear this out of my head :D. You can simply move the player's character gameObject constantly in the update function.
Of course I'm assuming you aren't using the built-in character motor script here. If you are then have a look at the scripting reference on how to interact with character motor using scripts. And then use pretty much the same method as I will show now to move the player through that.
So enough rambling on, here's an example.
gameObject.transform.position += vector3.forward * speedMultiplier * time.Deltatime;
This line adds the vector3.forward (or z variable of a vector3) times the speedmultiplier times time.Deltatime to the character's position, moving it forward at a somewhat constant rate when each frame is rendered.
I would define speedMultiplier as a float (of course if you're doing java, it does it for you) Also I'm not certain whether it's += or -= the new position, you can play with that. But the vector3 you need should be forward, as long as your character is facing that way in the scene I guess.
Then all you need to do is play the running or whatever animation you have and match up the speed to the stepping (trial and error as far as I'm aware). You would need need input for the jump and slide, and you can make a little jump script and use the same method to move the character up and down using vector3.up (the y axis of a vector3), and sliding can use the slide animation, and increase the characters speedmultiplier variable during the animation.
I hope I was helpful.