Onouris
December 13, 2019, 8:25am
1
Hello,
I currently have a character who can move in different directions, for each direction a different animation is launched. The movement is currently done using the keys.
I would like that depending on the angle of the mouse around the character the meaning of the animation also changes.
I need that depending on the angle of the mouse, the animation of the direction starts. As the example below.
[Header("Movement")]
[Tooltip("Walk movement")]
public float speed = 5f;
[Tooltip("Player Rigidbody")]
public Rigidbody2D rigidBody;
public Animator animator;
Vector2 movement;
void FixedUpdate() {
// Position
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
// Animations
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Vitesse", movement.sqrMagnitude);
// Angle isometric
if (movement.x != 0 && movement.y != 0)
{
movement.y = movement.y / 2;
}
Vector2 inputVector = new Vector2(movement.x, movement.y);
inputVector = Vector2.ClampMagnitude(inputVector, 1);
Vector2 movement = inputVector * speed;
// Movement
rigidBody.MovePosition(rigidBody.position + movement * Time.fixedDeltaTime);
}
If anyone has a lead or an example, I thank you in advance.
eses
December 13, 2019, 1:55pm
2
Hi @Onouris
Do you really need angle?
You can get angle between character pivot and mouse click in screen space, and convert that into angle value, which Unity supports straight out of the box:
But I guess just using direction vector instead of input axis as input could also work for your blend tree?
Onouris
December 13, 2019, 3:26pm
3
eses:
Hi @Onouris
Do you really need angle?
You can get angle between character pivot and mouse click in screen space, and convert that into angle value, which Unity supports straight out of the box:
https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html
But I guess just using direction vector instead of input axis as input could also work for your blend tree?
Yes, I need it then for the direction animations in the blend tree
eses
December 13, 2019, 3:27pm
4
@Onouris
“Yes, I need it then for the direction animations in the blend tree”
Need vector or angle?
Onouris
December 13, 2019, 3:51pm
5
@eses
I need to convert this to float value like for Input.GetAxisRaw(“Horizontal”).
In order to return a value between -1 and 1, to start the animation in the blend tree.
Onouris
December 13, 2019, 9:47pm
6
SOLUTION
Here is the code that solves this problem:
// Direction towards cursor
Vector3 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 towardCursor = mouse - transform.position;
// Angle between worldspace right and direction towards cursor
// SignedAngle to detect negative angles
float angleVertical = Vector2.SignedAngle(transform.right, towardCursor);
// Angle between worldspace up and direction towards cursor
float angleHorizontal = Vector2.SignedAngle(-transform.up, towardCursor);
if (angleVertical > 90 || angleVertical < -90) {
angleVertical = angleVertical > 0 ? 180 - angleVertical : -180 - angleVertical;
}
if (angleHorizontal > 90 || angleHorizontal < -90) {
angleHorizontal = angleHorizontal > 0 ? 180 - angleHorizontal : -180 - angleHorizontal;
}
animator.SetFloat("Vertical", angleVertical / 90);
animator.SetFloat("Horizontal", angleHorizontal / 90);
1 Like