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 Grendel_Demon · Apr 20, 2014 at 01:13 AM · animation2dcontrollerspaceshipmovment

How to play correct animation based on 2d movement?

Hi all, I am making a 2d side scrolling shooter, and have a problem with linking my thruster animation with the ships forward, back, up and down movement. I have watched several tutorials, but all have only shown me how to move the player by the "vertical/Horizontal translation", not specifically "up" and "down", "left" and "right". Below is my player script, I am coding in C#, and an illustration of my problem...

alt text

 using UnityEngine;
 
 
 public class PlayerScript : MonoBehaviour
 {
     
     public bool facingRight = true;            // For determining which way the player is currently facing.
     
 
     public float moveForce = 40f;            // Amount of force added to move the player left and right.
     public float maxSpeed = 5f;                // The fastest the player can travel in the x axis.
     
     Animator  anim;
     
     void Start ()
     {
         anim = GetComponent<Animator>();
         
     }
     
     void Update()
         
     {
         
     }
     
     void FixedUpdate()
     {
         // Cache the horizontal input.
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         
         // The Speed animator parameter is set to the absolute value of the horizontal input.
         anim.SetFloat("Speed", Mathf.Abs(h));
         anim.SetFloat("Up", Mathf.Abs(v));
 
 
         
         // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
 
         if(h * rigidbody2D.velocity.x < maxSpeed)
             if(v * rigidbody2D.velocity.y < maxSpeed)
             // ... add a force to the player.
             rigidbody2D.AddForce(Vector2.right * h * moveForce);
         rigidbody2D.AddForce(Vector2.up * v * moveForce);
         
         // If the player's horizontal velocity is greater than the maxSpeed...
         if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
 
             // ... set the player's velocity to the maxSpeed in the x axis.
             rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
         
         
     }
     
 }
 




I hope I was concise in my explanation, and any advice would be most helpful :3

movment problem.jpg (163.6 kB)
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pyrian · Apr 20, 2014 at 11:35 PM

The reason your animations work for right and up but not left or down is because you're passing the absolute values of your input. This converts any left or down input into right or up input, respectively.

Comment
Add comment · Show 3 · 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 Grendel_Demon · Apr 20, 2014 at 11:49 PM 0
Share

I see. So do you know how my code should look, or were I may go for a tutorial?

avatar image Pyrian · Apr 21, 2014 at 03:39 AM 0
Share

I'd have to see your Animator transitions to know for sure, but I think if you just remove the $$anonymous$$athf.Abs functions on lines 34-35, passing in h & v unmodified, you might be golden. Those SetFloat's are the only calls to your Animator, and the $$anonymous$$athf.Abs is certainly killing any directional information from getting through.

avatar image Grendel_Demon · Apr 22, 2014 at 01:41 AM 0
Share

Ah ha! Eureka! I figures It out thanks to your observation and I will post my new script and a description of the solution. Thank you for the help! ^_^

avatar image
0

Answer by coolbird22 · Apr 20, 2014 at 05:42 AM

Try using this Java to C# converter - Java to C# converter Do note that you might have to rename your variable type and/or classes.

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 Grendel_Demon · Apr 20, 2014 at 10:01 PM 0
Share

Thanks for the recommendation, I will look into that ^-^

avatar image
0

Answer by Grendel_Demon · Apr 22, 2014 at 02:14 AM

To fix my problem, I created 4 different animator parameters for the 4 directions I wont the ship to animate in. I then blended between the idle state using the appropriate float parameters. to link this all up, I changed my mathf.Abs to mathf.Min, thanks to Pyrian's observation. I also had to update my new animator parameters in the script, and add a -v and -h parameter. Here is what my script looks like now;

 using UnityEngine;
 
 
 public class PlayerScript : MonoBehaviour
 {
 
     public bool facingRight = true;            // For determining which way the player is currently facing.
     
 
     public float moveForce = 40f;            // Amount of force added to move the player left and right.
     public float maxSpeed = 5f;                // The fastest the player can travel in the x axis.
     
     Animator  anim;
     
     void Start ()
     {
         anim = GetComponent<Animator>();
         
     }
     
     void Update()
         
     {}
     
     void FixedUpdate()
     {
         // Cache the horizontal input.
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         
         // The Speed animator parameter is no longer set to the absolute value of the horizontal or vertical input.
         anim.SetFloat("Right", Mathf.Min (h));
         anim.SetFloat("Up", Mathf.Min (v));
         anim.SetFloat("Down", Mathf.Min (-v));
         anim.SetFloat("Left", Mathf.Min (-h));

 
         if(h * rigidbody2D.velocity.x < maxSpeed)
             if(v * rigidbody2D.velocity.y < maxSpeed)
             // ... add a force to the player.
             rigidbody2D.AddForce(Vector2.right * h * moveForce);
         rigidbody2D.AddForce(Vector2.up * v * moveForce);
         
         // If the player's horizontal velocity is greater than the maxSpeed...
         if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
 
             // ... set the player's velocity to the maxSpeed in the x axis.
             rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
         
     
     }
     
 }

 
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

22 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 avatar image avatar image avatar image

Related Questions

Help with Top-Down Controls using 2D Toolkit 2 Answers

Unity 2D Play animation by time event 0 Answers

Problem with character movement 2d when attaching animator 1 Answer

Animation Problems using 2D Toolkit 2 Answers

How to use multiple bone rigged sprites for 2d character animations 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