- Home /
2D Simple movement script (JS)
I know that this is not a question, but this is the best place for it I am very sorry!
Hello, so fathers day was coming up quickly and I wanted to do something special for him, so just like every other person normally would, I made a video game for him. It was a simple video game, well as simple as they get. It only had 195 lines of code in total from the 4 scripts. I only had 2 days to make a fun and small game for my father, so I was looking for a simple 2D movement script that could be easily edited that I could use. I searched for a while and found many 2D movement scripts but none that were quick and easy to use, so I got pissed off and made one my self. I made this post so others could easly see it and stop the frustration. The script doesn't explain how to set up animations to work with it because that is a easy find. Within the script I break it into parts, and I tell you what you need to do to set the script up and tell you what you need to change in order to move faster, jump higher, or trigger your animations. Here is the script.
#pragma strict
//IMPORTANT::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::IMPORTANT
//YOUR SPRITE NEEDS TO BE FACING LEFT
//YOU JUST NEED THE SPRITE IN THE GAME AND THIS SCRIPT WITH AN ANIMATOR
//THE SPRITE STAYS ON ONE X POSITION UNLESS YOU CHANGE THE START HEIGHT
//THIS IS SET UP IN A EXTREAMLY SIMPLE WAY FOR BEGINERS NOT A PRO SCRIPT
//IT IS DONE THIS WAY FOR YOU TO LEARN AND HAVE A SIMPLE SCRIPT TO TEST WITH
//IMPORTANT::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::IMPORTANT
//Public var's
public var maxMovmentSpeed : float = 0.1; //This sets the maximum speed you can go.
public var jumpIncrese : float = 0.4; //This will change how high you jump.
public var idleToMaxWalk : float = 0.005; //This changes the rate you go from idle to max walk speed.
public var jumpAnimationName : String = "Jump"; //This should be the name of jump trigger in the animator.
public var walkAnimationName : String = "Walk"; //This should be the name of walk trigger in the animator.
//Private var's
private var leftIncrese : float = 0.0; //DO NOT CHANGE THIS!
private var rightIncrese : float = 0.0; //DO NOT CHANGE THIS!
private var isJumping : boolean = false; //DO NOT CHANGE THIS!
private var startHeight : float; //DO NOT CHANGE THIS!
private var valueHolder : float; //DO NOT CHANGE THIS!
private var anim : Animator; //DO NOT CHANGE THIS!
//-----------------------------------------------------------\\
function Start () {
//transform.position = Vector3(1, 0, 0);
startHeight = transform.position.y; //This sets a variable equal to the starting height of the sprite.
valueHolder = -jumpIncrese; //This will be used to end the jump at the same y it started on.
anim = GetComponent(Animator); //This is used to set triggers in the animator you should know this.
}
//-----------------------------------------------------------\\
//IMPORTANT::::YOU NEED YOUR SPRITE TO FACE LEFT AT START!!\\
function Update () {
//~~~~~~~~~~~~~~~~~Walk Animation~~~~~~~~~~~~~~~~~~\\
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A)) {
anim.SetTrigger (walkAnimationName);
} else {
anim.SetTrigger ("StopWalk"); //StopWalk is the name of a trigger to sto the walking animation.
}
//~~~~~~~~~~~~~~~~~Walk Animation~~~~~~~~~~~~~~~~~~\\
//~~~~~~~~~~~~~~~~~~~~~~Left~~~~~~~~~~~~~~~~~~~~~~~\\
if (Input.GetKey(KeyCode.D)) {
if (rightIncrese < maxMovmentSpeed) {
rightIncrese += idleToMaxWalk;
transform.rotation.y = 180; //This will make the sprite switch direction depending on the direction
}
transform.position.x += rightIncrese;
} else {
rightIncrese = 0.0;
}
//~~~~~~~~~~~~~~~~~~~~~~Left~~~~~~~~~~~~~~~~~~~~~~~\\
//~~~~~~~~~~~~~~~~~~~~~Right~~~~~~~~~~~~~~~~~~~~~~~\\
if (Input.GetKey(KeyCode.A)) {
if (leftIncrese < maxMovmentSpeed) {
leftIncrese += idleToMaxWalk;
transform.rotation.y = 0; //This will make the sprite switch direction depending on the direction
}
transform.position.x -= leftIncrese;
} else {
leftIncrese = 0.0;
}
//~~~~~~~~~~~~~~~~~~~~~Right~~~~~~~~~~~~~~~~~~~~~~~\\
//~~~~~~~~~~~~~~~~~~~~~Jump~~~~~~~~~~~~~~~~~~~~~~~\\
if (isJumping == false) {
if (Input.GetKeyDown(KeyCode.W)) {
anim.SetTrigger (jumpAnimationName);
isJumping = true;
}
}
if (isJumping == true) {
transform.position.y += jumpIncrese;
jumpIncrese -= Time.deltaTime;
if (jumpIncrese < valueHolder) {
jumpIncrese = -valueHolder;
isJumping = false;
transform.position.y = startHeight;
}
}
}
//-----------------------------------------------------------\\
I hope this helps some of you!
This really isn't the best place for it :D
You should rather post this script on your blog, twitter, reddit, forums. There other people might see it and find it if they are looking for it.
Answer by warriorplayzbeno22 · May 10, 2019 at 09:24 PM
TYSM I NEEDED THIS OMG I AM IN YOUR DEBT ETERNALLY, can you make a simple one just for jumping and moving left and right for me? i need one for my game im making in c#. still cant find a good tutorial, this is the best one and since im a nub im still confused
Your answer
Follow this Question
Related Questions
Limiting Player Movement in 2D game? 0 Answers
Why is my character controller not working? 1 Answer
2D rigidbody movement & walls 2 Answers
[2D] CharacterController rotation 0 Answers