- Home /
Basic mario style platform walking script
I want to make a basic walking script similar to a mario style game. I want the character to only be able to walk forward and backwards, and to be able to jump. I took a break from scripting so im kinda rough around the edges if anyone could help me out or reference a wiki that would be great. -Thanks
Answer by Kristov · Dec 02, 2010 at 05:12 AM
A tutorial for 2D gameplay is here: http://unity3d.com/support/resources/tutorials/2d-gameplay-tutorial . It's about everything to do with making a 2D game, but I don't think it walks you through the scripting process. Since it has an example project though, you can take a look at the scripts they use and how they work.
Good luck!
Answer by Rennat · Dec 02, 2010 at 07:36 AM
here's one approach that doesn't use physics or a character controller:
this script would go on your character (after you finish it)
public var runSpeed : float = 3; public var runSpeedDamping : float = 0.5; public var jumpSpeed : float = 4; // I made these values up, public var fallSpeed : float = 4; // they will need to be tweaked
private var isGrounded : boolean = false; private var velocity : Vector2 = Vector2.zero;
function Update () { // figure out if you're touching the ground // you could use a downward raycast or colliders/triggers isGrounded = ...
// figure out how fast to move based on input
velocity.x = Mathf.Lerp(velocity.x,
runSpeed * Input.GetAxis("Horizontal"),
runSpeedDamping);
// jumping/falling
if (isGrounded) {
// if you're on the ground, stop the vertical movement
velocity.y = 0;
if (Input.GetButtonDown("Jump")) {
// if you hit jump then give it some vertical speed
velocity.y = jumpSpeed;
}
} else {
// if you're not touching the ground, reduce your vertical
// speed a little bit every frame.
velocity.y -= fallSpeed * Time.deltaTime;
}
// apply the movement using only the X and Y axes
var movementVector : Vector3 = new Vector3(
velocity.x * Time.deltaTime,
velocity.y * Time.deltaTime,
0);
// You should use some raycasts here to make sure you won't collide
// with anything and if you do adjust your movement distance
// you can use movementVector.Normalize to get the direction of a ray
// and movementVector.magnitude to get the length for the ray
// if your raycast hits then `hit.distance * movementVector.Normalize`
// will give you the new movement vector
transform.Translate(movementVector);
}
Answer by Meltdown · Dec 02, 2010 at 11:23 AM
Also take a look at this link.. It contains 5 small 2-d game examples and goes through all the scripting.
Its in C# though...
http://forum.unity3d.com/threads/51017-5-Unity-game-examples-C-scripting-tutorial-for-beginners