• 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 acer777 · Feb 05, 2019 at 07:54 PM · animationmousemouse positionfacingrelative position

Mouse Location in world relative to the player

So I have a 2D pixel art character in a 3D enviroment using x and z to move about. I'm using blend trees and x and z locations to play animations based on direction, so I am currently track the mouse position relative to the player. It works on the x but not on the z (up and down the screen), it seems to slowly go out of sync I think its because of the camera being at a angle but I'm unsure. I'm using the x and z location of the mouse in the world relative to the player to play the correct facing animations upright, upleft, downright, downleft. Like I said x works perfectly and stays relative where as y or 3d space z doesn't.

I hope this makes sense and someone can help? Many thanks in advance.

Below is my script:

 public class PlayerRotation : MonoBehaviour {
 
     //Mouse Input variable (for offsetting)
     public Vector3 mouseInput;
 
     //Get animator
     public Animator anim;
 
     // Use this for initialization
     void Start () {
 
     }    
     
     // Update is called once per frame
     void Update () {
 
 
         //Get Mouse Position
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         //Get Player Position
         Vector3 PlayerPosition = transform.position;
         //Offset mouse offset -0.5f for offseting object in game
         mouseInput = new Vector3(mousePos.x - PlayerPosition.x, mousePos.y - PlayerPosition.z, mousePos.z - PlayerPosition.z);
 
         //If mouse is on left flip image
         if (mouseInput.x < 0) {
             //flip image left
             transform.GetChild(0).localRotation = Quaternion.Euler(0, 180, 0);
         } else {
             //else flip the image right
             transform.GetChild(0).localRotation = Quaternion.Euler(0, 0, 0);
         }
 
         //Set animation parameters for animations
         anim.SetFloat("xInput", mouseInput.x);
         anim.SetFloat("zInput", mouseInput.y);
 
         //Debugging purposes
         Debug.Log( PlayerPosition);
         Debug.Log( mouseInput);
 
  
     }
 }
Comment
Add comment · Show 1
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 toddisarockstar · Feb 05, 2019 at 09:22 PM 0
Share

If you are using 3d space I would think you would want to use a raycast into your scene from the mouse to get the actual point on the "floor" of your level. this would compensate for the angle of your floor.

https://docs.unity3d.com/ScriptReference/Collider.Raycast.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by blueshark- · Feb 05, 2019 at 10:39 PM

Is it on purpose that on mouseInput you do mousePos.y - PlayerPosition.Z? Instead of mousePos.y - PlayerPosition.Y

Comment
Add comment · 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
0

Answer by Ymrasu · Feb 05, 2019 at 11:02 PM

I gave an answer to this on your last question. You should raycast from your mouse to a plane in the world to account for the angle of the camera. Inserting it into your code should be this:

 public class PlayerRotation : MonoBehaviour {
  
      //Mouse Input variable (for offsetting)
      public Vector3 mouseInput;
  
      //Get animator
      public Animator anim;
  
      // Use this for initialization
      void Start () {
  
      }    
      
      // Update is called once per frame
      void Update () {
 
         // make a plane in the world level to the player
         Plane plane = new Plane(transform.up, transfrom.position);
         
         // create a ray from your mouse
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
         Vector3 mousePos = Vector3.zero;
         // cast the ray from mouse to the plane
         if(plane.Raycast(ray, out float distance)) {
              // get mouse position from where it hit in the world
             mousePos = ray.GetPoint(distance);
         }
 
 
          //Offset mouse offset -0.5f for offseting object in game
          mouseInput = mousePos - transform.position;
  
          //If mouse is on left flip image
          if (mouseInput.x < 0) {
              //flip image left
              transform.GetChild(0).localRotation = Quaternion.Euler(0, 180, 0);
          } else {
              //else flip the image right
              transform.GetChild(0).localRotation = Quaternion.Euler(0, 0, 0);
          }
  
          //Set animation parameters for animations
          anim.SetFloat("xInput", mouseInput.x);
          anim.SetFloat("zInput", mouseInput.z);
  
          //Debugging purposes
          Debug.Log( transform.position);
          Debug.Log( mouseInput);
  
   
      }
  }
Comment
Add comment · Show 1 · 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 acer777 · Feb 13, 2019 at 05:29 PM 0
Share

Hi sorry its been a while, it looks like Raycast (out) is deprecated in the latest version of unity c#.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

263 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

Related Questions

Object Look At Mouse 2 Answers

teeworlds eyes animation 2 Answers

Third person animation 0 Answers

Making a character point his gun at the mouse 2 Answers

Mouse Position to 2D array 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