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