Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by DubstepDragon · Mar 02, 2014 at 04:22 PM · charactermouserotatecursorface

Character always facing mouse cursor position...

Is it possible to always have the character rotate to face the mouse cursor position? As in similar to the AngryBots demo, except rotating the whole player? I think it is fairly simple, however an hour of research proved it not so. Can anyone help me with this?

Thanks in advance!

Comment
Add comment · Show 3
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 Destran · Mar 02, 2014 at 04:31 PM 2
Share

Not sure if this is what you needed, but this has worked for me. If you create an empty Gameobject then place that Gameobject in the center of your character(or whatever point you want your character to rotate around) then make your player a child of the Gameobject. And then attach the following script to your Gameobject:

 using UnityEngine;
 using System.Collections;
     
 public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
             
 void Update () {
             Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
             Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
             lookPos = lookPos - transform.position;
             float angle = $$anonymous$$athf.Atan2(lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     
         }
     }

It should rotate your character to always face your cursor.

P.S: sorry for the terrible formatting on the code, I don't know what happened.

avatar image DubstepDragon · Mar 02, 2014 at 05:01 PM 0
Share

Well, it's kinda what I wanted, but...

The character is only rotating around the X and Y axis, which is not what I wanted. I wanted it to rotate around the X and Z. Can you do that please? Thanks so much! :D

avatar image romzz · Mar 30, 2016 at 08:17 PM 0
Share

Destran's answer helped me with my problem but, making an empty Gameobject and taking your character inside that Gameobject isn't the best solution. What I did was, to put my character inside the script by adding the public Gameobject; And also changing the y in Atan to z, and chaning Vector3.forward to Vector3.down:

  using UnityEngine;
  using System.Collections;
      
  public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
              
  public Gameobject human;
  
  void Update () {
              Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);<br>
              Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
              lookPos = lookPos - human.transform.position;
              float angle = $$anonymous$$athf.Atan2(lookPos.z, lookPos.x) * $$anonymous$$athf.Rad2Deg;
              human.transform.rotation = Quaternion.AngleAxis(angle, Vector3.down);
          }
  }
 

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by TimCoster · May 29, 2016 at 07:28 PM

This works for me:

 void Update() 
 {            
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
     if(Physics.Raycast(ray,out hit,100))
     {
        transform.LookAt(hit.point);
     }
 }

Or:

 void Update() 
 {            
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
     if(Physics.Raycast(ray,out hit,100))
     {
         transform.LookAt(new Vector3(hit.point.x,transform.position.y,hit.point.z));
     }
 }

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 DesenvolvedorOpenWorld · Oct 16, 2017 at 12:00 AM 1
Share

Hey buddy, my character he follows the command correctly. but he has a problem because when he goes to walk he keeps looking at the Y axis, and I wanted him to look only at the X and Z. Your script was as close as possible to making an isometric game. Please help me. void Update() {
RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

  if(Physics.Raycast(ray,out hit,100))
  {
      transform.LookAt(new Vector3(hit.point.x,transform.position.y,hit.point.z));
  }

}

avatar image Slastraf · May 29, 2018 at 05:26 PM 1
Share

however this does use alot more resources, and it requires to have something in the background to hit.

avatar image
2

Answer by nasoukikos · Mar 02, 2014 at 04:44 PM

try this and call the function when you want it

 private Vector3 worldpos;
     private float mouseX;
     private float mouseY;
     private float cameraDif;
 
     public GameObject fpc;
 
 void Start()
 {
 cameraDif = camera.transform.position.y - fpc.transform.position.y;
 }
 
 void LookAtMouse()
     {
         mouseX = Input.mousePosition.x;
         
         mouseY = Input.mousePosition.y;
         
         worldpos = camera.ScreenToWorldPoint(new Vector3(mouseX, mouseY, cameraDif));
         
         Vector3 turretLookDirection = new Vector3 (worldpos.x,fpc.transform.position.y, worldpos.z);
         
         fpc.transform.LookAt(turretLookDirection);
     }
 
 

 
Comment
Add comment · Show 6 · 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 DubstepDragon · Mar 02, 2014 at 05:27 PM 0
Share

This script is already functional:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
     
     void Update () {
         Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
         Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
         lookPos = lookPos - transform.position;
         float angle = $$anonymous$$athf.Atan2(lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         
     }
 }



However it just rotates me the wrong way... I want the rotation to be around the X and Z, not X and Y.

avatar image nasoukikos · Mar 02, 2014 at 06:13 PM 1
Share

where it says input.mouseposition.y just replace the y with the z or whatever you want

avatar image DubstepDragon · Mar 02, 2014 at 08:20 PM 0
Share

Can you please change it for me? I am kinda new to mouse position and Vector3 positions. Thanks! :D

avatar image nasoukikos · Mar 02, 2014 at 11:39 PM 2
Share

try this

using UnityEngine; using System.Collections;

 public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
  
     void Update () {
        Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
        Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
        lookPos = lookPos - transform.position;
        float angle = $$anonymous$$athf.Atan2(lookPos.z, lookPos.x) * $$anonymous$$athf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  
     }
 }
avatar image someguywithamouse nasoukikos · Jun 01, 2018 at 03:34 PM 0
Share

I was gonna say the same thing... Yeah pretty easy.

@DubstepDragon Just so you know, when it comes to languages like JS and C#, remember that if you see ANY method that returns a value or object followed by a period and the characters X, Y, or Z, then feel free to change it. Here's why:

Usually in those situations, when there's a dot right after "Input.mousePosition" (or anything returning data), then whatever goes after that dot is either another method that does something with that object, or a certain property. In this case, it's a property because it's the X, Y and Z axis, all of which are for measuring position rotation and size.

Sorry for the long explanation, just thought I might explain and try to help you understand in the future, since you said you were new.

avatar image someguywithamouse · Jun 01, 2018 at 03:40 PM 0
Share

@nasoukikos I have some questions about this code:

  private Vector3 worldpos;
      private float mouseX;
      private float mouseY;
      private float cameraDif;
  
      public GameObject fpc;
  
  void Start()
  {
  cameraDif = camera.transform.position.y - fpc.transform.position.y;
  }
  
  void LookAt$$anonymous$$ouse()
      {
          mouseX = Input.mousePosition.x;
          
          mouseY = Input.mousePosition.y;
          
          worldpos = camera.ScreenToWorldPoint(new Vector3(mouseX, mouseY, cameraDif));
          
          Vector3 turretLookDirection = new Vector3 (worldpos.x,fpc.transform.position.y, worldpos.z);
          
          fpc.transform.LookAt(turretLookDirection);
      }
  



I haven't tested your code in my game yet, but just to clarify: this does work for 2D top-down shooters and platformers, etc. if I understand correctly? Or do I have change it up a little bit to (litterally) get it heading the right direction?

avatar image
0

Answer by zozan323 · Dec 06, 2019 at 10:39 AM

@TimCoster thanks this has solved my issues.

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

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

27 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

Related Questions

unity2D: make object face mouse 2 Answers

Character - Move In Mouse Direction 1 Answer

Rotate the player to face where the mouse is pointing 3 Answers

Movement along X and Z axis... 2 Answers

Shooting in direction of mouse cursor 2d 5 Answers


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