Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Aigean · Jan 04, 2020 at 11:17 AM · 2d2d game2d sprites2d animation

How can I change player sprite based on mouse position relative to the player?

So, I have 2d scene and want player to switch between idle sprites based on where he's looking (i.e. where is mouse cursor relative to player). I set up an "Idle" blend tree, which switches idle state sprites for 8 directions with parameters "Mouse Horizontal" and "Mouse Vertical". Howewer, blend tree reads mouse coordinates only when I move the mouse. When I move mouse to the left it switches to "look_left" sprite, even if mouse is to the right of the player. When I'm not moving mouse it just uses first sprite in blend tree.

How can I find mouse position relative to the player and use it to swich sprites?

Here is the code i used:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public float moveSpeed = 5f;
     public Rigidbody2D rb;
     public Camera cam;
     public Animator animator;
     Vector2 movement;
     Vector2 mousePos;
 
     void Update()
     {
         movement.x = Input.GetAxisRaw("Horizontal");
         movement.y = Input.GetAxisRaw("Vertical");
         mousePos.x = Input.GetAxisRaw("Mouse X");
         mousePos.y = Input.GetAxisRaw("Mouse Y");
 
         animator.SetFloat("Mouse Horizontal", mousePos.x);
         animator.SetFloat("Mouse Vertical", mousePos.y);
         animator.SetFloat("Move Horizontal", movement.x);
         animator.SetFloat("Move Vertical", movement.y);
         animator.SetFloat("Speed", movement.sqrMagnitude);
     }
 
     void FixedUpdate()
     {
         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
     }
 }
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

2 Replies

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

Answer by pako · Jan 05, 2020 at 09:17 AM

You must compare the mouse position given by Camera.main.ScreenToWorldPoint(Input.mousePosition) to the player's position given by rb.position.

By comparing the individual x,y coordinates obtained from these functions you will know if the mouse is to the left of the player (mouse.x < player.x), above the player (mouse.y > player.y), etc.

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 Aigean · Jan 05, 2020 at 05:25 PM 1
Share

Thank you very much! It worked!

Here's the code I ended up with, if anybody encounters this problem later:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
 {
     public float moveSpeed = 3f;
     public Rigidbody2D rb;
     public Camera cam;
     public Animator animator;
     Vector2 movement;
     Vector2 mousePos;
     Vector2 posDif;
 
 
     void Update()
     {
         movement.x = Input.GetAxisRaw("Horizontal");
         movement.y = Input.GetAxisRaw("Vertical");
         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
 
         posDif = mousePos - rb.position;
 
         animator.SetFloat("$$anonymous$$ouse Horizontal", posDif.x);
         animator.SetFloat("$$anonymous$$ouse Vertical", posDif.y);
         animator.SetFloat("$$anonymous$$ove Horizontal", movement.x);
         animator.SetFloat("$$anonymous$$ove Vertical", movement.y);
         animator.SetFloat("Speed", movement.sqr$$anonymous$$agnitude);
     }
 
     void FixedUpdate()
     {
         rb.$$anonymous$$ovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
     }
 }
avatar image
0

Answer by IchKannNixs · Jan 04, 2020 at 11:45 AM

try to use "Camera.main.ScreenToWorldPoint(Input.mousePosition);" than just the last movement of the mouse

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 Aigean · Jan 05, 2020 at 08:51 AM 0
Share

Thank you! It improved a bit - sprite now stays in last selected position insted of switching to first in tree. But still, it reads absolute mouse movement and not relative to the player. Anyway, thank you for your time!

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

256 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 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 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 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 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 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 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 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 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 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 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 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

How to sync animation if animation should be use with specially object 0 Answers

Suggestions for player evolution 1 Answer

[Solved] Does Unity's 2D Tilemap Editor 1.0.0 Include Rule Tiles? 1 Answer

2D Sprite Fillup without using UI 1 Answer

,Spawning snow or changing tilesets to snow as the player walks past them 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