Answer the question
In order to leave comments, you need to log in
How can sprites and animations change depending on the position of the 2D cursor. (In my case androud joystick)?
During development, there was a desire to make a character animation, the same as in the Enter The Gungeon game. That is, the hero has 8 sprites (in 8 directions) and animations (at least + stands) and this changes depending on the position of the cursor, in my case of a joystick under android I did something similar, BUT only when I press the keys. I would be grateful if you tell me how to add a mouse to this code (or change it completely). So that when you press the keys, only moving around the world occurs, and the animation and sprites change depending on the position of the cursor. It will be nice if you help optimize for android.
Knowledge in C # is small, but I want to implement the idea. I will be grateful! (The gif shows an example)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float moveSpeed = 3f;
Animator thisAnim;
float lastx, lasty;
void Start()
{
thisAnim = GetComponent<Animator>();
}
void Update()
{
Move();
}
void Move()
{
Vector3 rightMovement = Vector3.right * moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
Vector3 upMovement = Vector3.up * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical");
Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
transform.position += rightMovement;
transform.position += upMovement;
UpdateAnimation(heading);
}
void UpdateAnimation(Vector3 dir)
{
if (dir.x == 0f && dir.y == 0f)
{
thisAnim.SetFloat("LastDirX", lastx);
thisAnim.SetFloat("LastDirY", lasty);
thisAnim.SetBool("Movement", false);
}
else
{
lastx = dir.x;
lasty = dir.y;
thisAnim.SetBool("Movement", true);
}
thisAnim.SetFloat("DirX", dir.x);
thisAnim.SetFloat("DirY", dir.y);
}
}
Answer the question
In order to leave comments, you need to log in
You create a point, make the object a child of it or bind it explicitly along one of the coordinate axes (as an option, you can use a unit vector in global coordinates), determine the angle between this point by the hero and the crosshair, give the desired sprite by the angle.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question