- Home /
Question by
that_brazillian_guy · May 06, 2020 at 01:25 PM ·
2danimationsmouseposition
change animations based on mouse position
i am trying to get my 2d top- down game character changing idle animations based on where my mouse is, a good example of this would be "enter the gungeon", this is the code i tried to make work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement_mouse : MonoBehaviour
{
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("Mouse Horizontal", posDif.x);
animator.SetFloat("Mouse Vertical", posDif.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);
}
}
keep in mind that im new to unity and C#, and im trying to learn all i can, so there will be painfully obvious mistakes here
Comment
Your answer
Follow this Question
Related Questions
Multiple Sprite animations 1 Answer
How to stop an object of going to far 2 Answers
Instantiate gameobject at mouseposition 2d 2 Answers
Animated 2D mask ?? 1 Answer
Why is my raycast2D offset from the mouse position? 0 Answers