- Home /
Combine animation and mouse position
I'm trying to animate my character but i want to go through the animation according the mouse position. In the game, the player will try to catch some fish spawned on a random position. The player has an animation who show im bending on a side but i want to bend the character following the mouse position. For the moment i've written a script who launch the animation depending on the position of the mouse but it don't work as i want :
public float screenPart;
Animator anim;
private void Start() {
screenPart = Screen.width / 3;
anim = GetComponent<Animator>();
}
void Update()
{
//If the game is not paused
if (Time.timeScale == 1)
{
//If the mouse is in the bottom screen
if (Input.mousePosition.y <= Screen.height/2)
{
//If mouse is clicked
if (Input.GetMouseButton(0))
{
if (Input.mousePosition.x <= screenPart)
{
anim.SetInteger("State", 0);
Debug.Log("Left");
}
if (Input.mousePosition.x >= screenPart && Input.mousePosition.x <= screenPart * 2)
{
anim.SetInteger("State", 1);
Debug.Log("Middle");
}
if (Input.mousePosition.x >= 2 * screenPart)
{
anim.SetInteger("State", 2);
Debug.Log("Right");
}
}
I was thinking about set the animation to 1s and then jump to a keyframe with the mouse position, but i don't know if it's the right approach of the problem. I've started to use a blend tree but how modify the value with the mouse position ? And this method will follow the mouse pointer ?
I don't know if my description is enough, tell me if want more details.
Nobody knows ? I advanced in my problem and i'm using a blend tree to smooth the transition betwen poses. I'm newbie in animation and unity so it's pretty hard for me :) Thanks for help
What I understand is that your character has 3 poses, Idle, bend to the left and bend to the right, and you are now using a blend tree that goes from -1 (left) to 1 (right). Is it like that?
Yes it's that and i want to modifiy the blend according to the mouse position and so make the character follow the mouse for collecting fishes
Answer by tadadosi · May 27, 2020 at 03:50 PM
This should work. First make sure you got your blend tree setup like the image, them add the script to your player, then add your animator to the public variable and hit play to test it.
Code:
using UnityEngine;
public class AnimBlendWithMousePosition : MonoBehaviour
{
// Change this value to make it blend slower or faster
public float blendMultiplier = 2f;
public Animator _Animator;
[SerializeField] private float mousePosX;
private void Update()
{
mousePosX = GetMousePosX_As_NegativeOneToPositiveOne();
// Your animator should have a parameter named Blend or change this string to match
// the name or your paramenter
if (_Animator != null)
_Animator.SetFloat("Blend", mousePosX * blendMultiplier);
else
Debug.LogError(gameObject.name + " | Animator is missing!");
}
private float GetMousePosX_As_NegativeOneToPositiveOne()
{
// Store mouse pixel coordinates
float a = Input.mousePosition.x;
// Convert coordinates to a value that goes from (-Screen.width/2, +Screen.width/2)
float b = a - Screen.width / 2;
// Convert b into a value that goes from (-1, 1)
float c = b / Screen.width * 2;
return c;
}
}
I've not tested it yet bur i was doing a similar thing since last time i've posted. But you have added some parts of code very usefull. Thanks for help guys ;)
Your answer
Follow this Question
Related Questions
teeworlds eyes animation 2 Answers
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers