• 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
Question by Olgo · Jul 26, 2012 at 05:00 PM · character movementsprite animationex2d

How should I name cardinal directions to correspond to character movement?

Hi,

I'm new to programming and have been piecing together sample code to create a game while I learn the ropes. I'm using EX2D to animate my sprites. I'm trying to use the following script to assign a specific animation to each direction the character moves while up, down, left, or right is pressed. How should I name Vertical and Horizontal in the GetButton statements to correspond to each cardinal direction?

Right now, the only error I get is "Object reference not set to an instance of an object"

Thanks!

 var sprite: exSprite;
 
     function Start () {
 
     }
 
     function Update () {
 
 while(Input.GetButton("Vertical"))
  { 
  sprite.spanim.Play("PlayerWalkUpAnimation");
  }
 while(Input.GetButton("Vertical"))
  { 
  sprite.spanim.Play("PlayerWalkDownAnimation");
  }
 while(Input.GetButton("Horizontal"))
  { 
  sprite.spanim.Play("PlayerWalkLeftAnimation");
  }
 while(Input.GetButton("Horizontal"))
  { 
  sprite.spanim.Play("PlayerWalkRightAnimation");
  }
 
 }
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Kiloblargh · Jul 26, 2012 at 05:19 PM

Use Input.GetAxis, and define the buttons used in the Input Manager.

Comment

People who like this

0 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 Olgo · Jul 26, 2012 at 05:21 PM 0
Share

Thanks, if I'm only using Horizontal and Vertical, how will it differentiate between up/down or left/right?

avatar image

Answer by Ingen · Jul 26, 2012 at 05:27 PM

you can do it in Input Manager

Comment

People who like this

0 Show 2 · 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 Olgo · Jul 26, 2012 at 05:36 PM 0
Share

Are you referring to the "Descriptive Name?" If I change my code to read:

while(Input.GetAxis("Up")) { sprite.spanim.Play("PlayerWalkUpAnimation");

I get an "Input Axis Up is not setup" message. Even though I changed the Descriptive name to Up in the Input Manager.

avatar image Ingen · Jul 28, 2012 at 03:52 PM 0
Share

rising size you can also add a new key, and choose alphanumeric key you want use

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Instantiating projectile object is affecting the movement of instantiater 1 Answer

2D character stop immediately even when input buttons pressed 0 Answers

My character cant jump 2 Answers

Having trouble turning a Transform movement into a Rigidbody force. Code included 1 Answer

HOW DO I MAKE MY CHARACTER GAIN SPEED GRADUALLY 3D 2 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