- Home /
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);
}
}
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.
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);
}
}
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
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
Follow this Question
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