• 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 /
  • Help Room /
avatar image
0
Question by monaim-mtl · Jan 26 at 01:24 PM · 2d2d gameunity 2d

how to make my 2d top down character face the direction of the cursor

I am trying to make my character face the direction of the cursor and i already made him face the direction of the movements but when i use the same logic for the cursor it doesn't work for me .

Not sure what am i doing wrong.

 public class PlayerMovements : MonoBehaviour
 {
     [SerializeField] Camera _camera;
     [SerializeField] Transform _crosshair, _interactor;
     [SerializeField] Animator _animator;
     [SerializeField] float _playerSpeed = 5f, _crosshairDistance = 1f;
     [SerializeField] Rigidbody2D _rigidbody;
     Vector2 _movements, _mouseWorldPos;
     void Update ()
     {
         float inputHorizontal = Input.GetAxisRaw("Horizontal");
         float inputVertical = Input.GetAxisRaw("Vertical");
         Vector3 mouseScreenPos = Input.mousePosition;
 
         _movements.x = inputHorizontal;
         _movements.y = inputVertical;
         
         _animator.SetFloat( "Horizontal" , _movements.x );
         _animator.SetFloat( "Vertical" , _movements.y );
         _animator.SetFloat( "Speed" , _movements.sqrMagnitude );
         
         if( inputHorizontal==1 || inputHorizontal==-1 || inputVertical==1 || inputVertical==-1 )
         {
             _animator.SetFloat( "LastHorizontal" , inputHorizontal );
             _animator.SetFloat( "LastVertical" , inputVertical );
         }
 
         if( inputHorizontal>0 ) _interactor.localRotation = Quaternion.Euler(0,0,90);
         if( inputHorizontal<0 ) _interactor.localRotation = Quaternion.Euler(0,0,-90);
         if( inputVertical>0 ) _interactor.localRotation = Quaternion.Euler(0,0,180);
         if( inputVertical<0 ) _interactor.localRotation = Quaternion.Euler(0,0,0);
         
         _mouseWorldPos = _camera.ScreenToWorldPoint( mouseScreenPos );
         
         if( mouseScreenPos.x==1 || mouseScreenPos.x==-1 || mouseScreenPos.y==1 || mouseScreenPos.y==-1 )
         {
             _animator.SetFloat("LastHorizontal", mouseScreenPos.x);
             _animator.SetFloat("LastVertical", mouseScreenPos.y);
         }
     }
     void FixedUpdate ()
     {
         _rigidbody.MovePosition( _rigidbody.position+_movements.normalized*_playerSpeed*Time.fixedDeltaTime );
         Vector2 lookingDirection = _mouseWorldPos - _rigidbody.position;
         _crosshair.localPosition = lookingDirection * _crosshairDistance;
     }
 }


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 andrew-lukasik · Jan 26 at 02:10 PM

unity 2D look at cursor

 using UnityEngine;
 public class PlayerMovements : MonoBehaviour
 {
     [SerializeField] Camera _camera;
     [SerializeField] Transform _crosshair, _interactor;
     [SerializeField] Animator _animator;
     [SerializeField] float _playerSpeed = 5f, _crosshairDistance = 1.4f;
     [SerializeField] Rigidbody2D _rigidbody;
     Vector2 _input, _mouseWorldPos;
     void Update ()
     {
         // read inputs:
         _input.x = Input.GetAxisRaw("Horizontal");
         _input.y = Input.GetAxisRaw("Vertical");
         Vector3 mouseScreenPos = Input.mousePosition;
         _mouseWorldPos = _camera.ScreenToWorldPoint( mouseScreenPos );
 
         // look at crosshair (XY plane):
         {
             Vector3 lookDir = _crosshair.position - _interactor.position;
             float lookDirAngle = Mathf.Atan2(lookDir.y,lookDir.x) * Mathf.Rad2Deg;// angle between lookDir and X+ axis
             float correctedLookDirAngle = -90f - lookDirAngle;// we want Z+ to be forward, not X+
             _interactor.rotation = Quaternion.Slerp(
                 _interactor.rotation ,
                 Quaternion.AngleAxis( correctedLookDirAngle , -Vector3.forward ) ,
                 10f * Time.deltaTime
             );
         }
         
         // update animator:
         _animator.SetFloat( "Horizontal" , _input.x );
         _animator.SetFloat( "Vertical" , _input.y );
         _animator.SetFloat( "Speed" , _input.sqrMagnitude );
         if( _input.x==1 || _input.x==-1 || _input.y==1 || _input.y==-1 )
         {
             _animator.SetFloat( "LastHorizontal" , _input.x );
             _animator.SetFloat( "LastVertical" , _input.y );
         }
         if( mouseScreenPos.x==1 || mouseScreenPos.x==-1 || mouseScreenPos.y==1 || mouseScreenPos.y==-1 )
         {
             _animator.SetFloat("LastHorizontal", mouseScreenPos.x);
             _animator.SetFloat("LastVertical", mouseScreenPos.y);
         }
     }
     void FixedUpdate ()
     {
         _rigidbody.MovePosition( _rigidbody.position+_input.normalized*_playerSpeed*Time.fixedDeltaTime );
         Vector2 lookingDirection = _mouseWorldPos - _rigidbody.position;
         _crosshair.localPosition = lookingDirection.normalized * _crosshairDistance;
     }
 }



gif-26012022-15-08-10.gif (378.0 kB)
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.

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

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

Problem with tracking multiple object with multitouch 0 Answers

How do I turn off the previous object? 1 Answer

how can I make my bubbles created by this wave spawner move as it is preset in its movement script, because now it only creates them in their respective spawn point, but it doesn't move to the end point. 0 Answers

2D Platformer Projectile wont face mouse position? 0 Answers

Why isn't my BoxCollider2D doing anything? 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