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
-1
Question by coolbird22 · May 01, 2014 at 08:19 AM · animationrotatedirection

Play different animations based on direction character is facing, while following the mouse.

I have a character in a top down game moving as per the motion of the mouse. How do I play the respective animation based on the direction, the character is facing at any moment ? Kindly note that my character sprite is not as if it is being seen from 90 degrees over its head, but as if the character is seen from its' side. So, I don't the game object to rotate, but just switch animations based on the direction it is facing. I tried doing Mathf.Abs to check for Horizontal and Vertical axes but it didn't work. Here is the code for my character movement based on the movement of the mouse.

     private Vector3 mousePosition;
     public float moveSpeed = 0.1f;
     float speed = 1.0f;
 
     void Start () {
         Screen.showCursor = false;
         }
         
     void Update () {
         if (Input.GetMouseButton(0)) {
             
             mousePosition = Input.mousePosition;
             mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
             transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
 }
 }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mikael-Gyth · May 01, 2014 at 08:46 AM

 vector2 direction = transform.position - mouseposition;

Will give you the direction and distance from the character towards the mouseposition. You can use this to determine where the mouse is in relation to the character.

if you only need to check left/right you can simply check if direction.x is positive or negative. This can also be combined with direction.y to check if the mouse is above or below the character.

BTW: Mathf.abs returns the absolute value of a specified number. So -1 would be 1, and 1 would be 1 also.

Comment
Add comment · 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 coolbird22 · May 01, 2014 at 09:03 AM 0
Share

I do want the direction to be checked in X as well as Y. How exactly do I use this code to assign an animation to the directions ? Also, this can work for the horizontal and vertical directions, but how would it be possible to assign an animation to a diagonal movement ?

 Play.SetFloat ("speed", vector2 direction = transform.position - mouseposition);

This gives an error that says - Unexpected symbol 'direction'

avatar image Mikael-Gyth · May 03, 2014 at 03:06 PM 0
Share

Are you using Animator to control the animations?

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

21 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

Related Questions

Playing animations by velocity for the correct direction. 0 Answers

Animated sprites disappear when rotating 0 Answers

Nav mesh agent moves backwards - How can I rotate gameobject without using an empty 1 Answer

Make the player go into the direction I am looking at 0 Answers

Can the animation editor create local rotational data? 3 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