Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tion-Gaming · Jun 22, 2015 at 07:14 AM · 2dmovementcharactercontroller

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KayelGee · Jun 22, 2015 at 11:14 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Background & sprites are smearing towards the player during movement. Help determine the cause and solution? 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges