• 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 DubstepDragon · Mar 02, 2014 at 06:41 PM · movementmouserotatecursorlook

Movement along X and Z axis...

I want to rotate my character to face the mouse cursor. The script here does not do that, it rotates across X and Y. I want it to rotate across X and Z only. Can anyone help me?

The script is here:

 using UnityEngine;
 using System.Collections;
  
 public class MouseLook : MonoBehaviour {
  
 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 = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  
         }
     }

Thanks in advance! :D

Comment
Add comment · Show 5
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 NoahConstable · Mar 03, 2014 at 12:12 AM 1
Share

I'm slightly confused, do you want your character to spin as if it were turning to face your cursor? If so, that means that you want it to rotate only on the "Y" axis. I might be able to help, but I need more information.

avatar image DubstepDragon · Mar 03, 2014 at 07:57 AM 0
Share

Wow I've not explained it properly :P

Yeah is pretty much what you've said, I want one of its sides to always face the cursor, hence using only the Y axis. If you can help me with that, it'll be great :D

avatar image Destran · Mar 03, 2014 at 04:23 PM 0
Share

If you just replace the (lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;

With (lookPos.z, lookPos.x) * $$anonymous$$athf.Rad2Deg;

Then it should rotate along the x and z and not the y.

So something like 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);
  
         }
     }


Let me know if that works:D

avatar image robertbu · Mar 03, 2014 at 06:33 PM 0
Share

@Destran's answer is close, you also have to replace the Vector3.forward with 'Vector3.up' in the last line.

   transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
avatar image DubstepDragon · Mar 03, 2014 at 07:05 PM 0
Share

It's closer than the others so far... However I still need it to face the cursor whilst it moves in 360 degrees. With this script + robertbu's contribution, it only rotates half-way, and mirrors that for the other half.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by CodeElemental · Mar 03, 2014 at 08:26 AM

 void Update () {
            Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
            Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
            transform.LookAt(lookPos);
  
         }


This should work.

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 NoahConstable · Mar 03, 2014 at 08:29 AM 1
Share

I was actually about to post (almost) exactly that! Well hope it works, Dubstep.

avatar image DubstepDragon · Mar 03, 2014 at 04:02 PM 0
Share

It works... kinda.

It ins$$anonymous$$d keeps one face looking at the cursor... However, I want it just to rotate around the Y-axis, similar to the AngryBots demo, but only in rotation. I think your script works the right way, however I need it to only rotate around the Y. I tried freezing the rotation with a RigidBody, however it does not work :'(

Can you help me with this please? :D

avatar image DubstepDragon · Mar 03, 2014 at 04:06 PM 0
Share

It's a top-down game :D

avatar image CodeElemental · Mar 03, 2014 at 04:08 PM 1
Share

If you're using some form of isometric perspective, it's possible that you need to swap the Y and Z coordinates.

Can you elaborate a bit more on what behaviur are you getting , i can't understand the one-face looking part?

avatar image DubstepDragon · Mar 03, 2014 at 06:21 PM 0
Share

I want the character to always be facing the cursor, via rotation. This is as in the character is standing. The script you gave me makes him face the air when the cursor is nearby. As I already suggested about twice, I want it similar to the AngryBots demo.

Thanks in advance! :D

Show more comments
avatar image
1

Answer by wibble82 · Mar 03, 2014 at 09:06 AM

The simplest solution to your problem has already been posted above - you take the mousePos, ask the camera for the position that corresponds to world space, then set the transform to look at that point. This however will cause your character to look directly at the mouse, meaning they may end up at an odd angle (for example, if the new position is high above the charachter, it'll end up 'lieing down'. A small modification to the earlier code might be:

 void Update () 
 {
     //get the mouse position and convert it to world space
     Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);     
     Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
 
     // clamp the y coordinate to the same height as the player so the character doesn't face upwards
     lookPos.y = transform.position.y; 
 
     //force the character to look at the player (specify the up vector as well to get best results!)
     transform.LookAt(lookPos,Vector3.up);
 }

Hope that helps!

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 DubstepDragon · Mar 03, 2014 at 04:00 PM 0
Share

Sorry, but the script doesn't work :'(

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

25 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

Related Questions

Mouse look/rotate 2 Answers

How can I make player move with cursor?,How can I make player move with mouse? 1 Answer

Character always facing mouse cursor position... 3 Answers

Huh click and go script working 50% 0 Answers

Look at mouse 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