Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mrintoxx · May 26, 2020 at 10:40 AM · animationmouse position

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. alt text 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.

capture.png (350.7 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mrintoxx · May 26, 2020 at 08:08 PM 0
Share

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

avatar image tadadosi · May 26, 2020 at 10:12 PM 0
Share

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?

avatar image Mrintoxx tadadosi · May 27, 2020 at 04:40 AM 0
Share

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

avatar image Mrintoxx Mrintoxx · May 27, 2020 at 02:32 PM 0
Share

Anyone knows ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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. alt text


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;
     }
 }

parameters-to-blend-anim-tree-and-mouse-pos-unity.png (43.7 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mrintoxx · May 29, 2020 at 03:56 AM 0
Share

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 ;)

avatar image tadadosi Mrintoxx · May 29, 2020 at 04:07 AM 0
Share

Np! Glad I could help :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

318 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

teeworlds eyes animation 2 Answers

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Mouse Location in world relative to the player 2 Answers

Can I make animations snap to a frame? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges