• 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 largow · Feb 15, 2014 at 07:23 PM · movement scriptjumping2d animation2d physicsstopping-animation

Basic Movement on 2D (C#)

Hello, this is mi first time posting on this community as I installed and I ve been working working on Unity since yesterday. I started my first project which involves a character moving around some simple stage, like every platform game we know. The issue is, I ve been having some trouble with the code of the script referred to the basic movement of my character (moving and jumping properly). Despite being quite similar to Java, the programming language I know, I got some doubts about using C# on Unity.

For example, I got this character I told you, which is an animation (made with sprites) but obviously the animation never stops, its looping all the time. I've thought to make it stop when the character's not moving, or to change it when he's jumping but I don't really know how to make REFERENCE to my character. I mean, I don't know how to do something like this:

 if(myCharacter.isJumping()){
 
     myCharacter.setAnimation("Jump");

  //Or it could be   myCharacter.setSprite("Jump");

 }

It's difficult to me to see what my character is, and how to refer it inside my code. Same thing happens when I want to know if he's moving or not.

By the moment my codes just allows my character to move around the sceneraio without following mechanic's laws, as he can jump infinitely and falls really slow. He moves really fast as well... here's the few lines I could write:

 using UnityEngine;
 using System.Collections;
 
 public class Movimiento1 : MonoBehaviour {
 
     
     public Vector2 speed = new Vector2(50,50); 
     private Vector2 velocity;
 
     void Update () {
         
         
         float inputX = 0;
                 float inputY = 0;
         inputX = Input.GetAxis("Horizontal");
         inputY = Input.GetAxis("Vertical");
         
         //Ahora muevo segun la direccion
         // I already got the module(speed vector) and now I got the direction represented with the variables inputX and inputY, 
         //so now i cant mae the velocity vector:
         
         velocity = new Vector2(speed.x * inputX, speed.y * inputY);        
         
         
     void FixedUpdate() {
         
         rigidbody2D.velocity = velocity;
 
     }

In summary, my concerns are mainly referred to making reference to an specific GameObject in order to know it's actual variables, such as the Transform ones.

Sorry for my engish(i'm not a native speaker) and thank you in advance!

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 BOB_KSE · Nov 25, 2017 at 08:00 PM 0
Share

2D platformers are actually very complex with ray-casting or collision detection and what not.

Since you are new to unity, i recommend that you not start making games on your own games. ins$$anonymous$$d take a few months to learn the basics.

i recommend this online course, since you have some prior program$$anonymous$$g knowledge: https://www.udemy.com/unitycourse/

i learned a lot through this one course. hey, just one non native speaker to another :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BOB_KSE · Nov 25, 2017 at 07:51 PM

If you want to know the transform variables of a object you want to collide with in the game. Then, Add box colliders to both your character and the platform it wants to collide with.

Attach this code to your player Game object and insert desired values for speed and jump: https://github.com/katiSteven/Very-Simple-2D-Movement-in-unity/blob/master/OutputGuy.cs

and, Attach this one to your Platforms (ground on which the player/character will stand): https://github.com/katiSteven/Very-Simple-2D-Movement-in-unity/blob/master/Platform.cs

Comment
Add comment · Show 1 · 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
avatar image BOB_KSE · Nov 25, 2017 at 08:03 PM 0
Share

If you want to avoid using box colliders then, you can use Raycasting:

Follow this video tutorial for Basic 2D movement & Raycasting : https://youtu.be/$$anonymous$$bW$$anonymous$$8bCAU2w

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My player is lagging/stuttering when trying to move mid-air(after jumping) 1 Answer

Jump Animation 2D problems 0 Answers

How to keep 2D character facing the direction it was in(right/left), when it is moving up/down? 0 Answers

2D RPG Jumping 0 Answers

Cannot move and jump with rigid body. 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