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
1
Question by aclee · Apr 04, 2015 at 07:27 PM · camerajavascriptmovementobjectmecanim

rotate Y axis to face mouse

hi, i'm trying having a hard time making a script that will let my character always face the mouse, via the Y axis (top down style) but i am not having any luck. the hard part is that the camera is in a slight tilt so that it's not 100% "top down" (about a 40° tilt) so is there anyway to take the position of the mouse relative to the center of the screen (since the character is always center anyways) and translate that to rotation for the character?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Xarbrough · Apr 04, 2015 at 10:27 PM

You can go with your approach of getting the vector of your camera to the screen center and then setting your object's facing direction to the same vector. You might need to flip it to the correct orientation and you will have to come up with a solution what to do when the mouse is close to the center, because then it just becomes jittery.

 public class RotateByMouse : MonoBehaviour {
 
     public Transform gameObjectToRotate;
 
     Vector3 middleOfScreen;
 
     void Start() {
 
         middleOfScreen = new Vector3(Screen.width/2, Screen.height/2, 0f);
     }
 
     void Update() {
 
         Vector3 camVec = Input.mousePosition - middleOfScreen;
         Vector3 flipped = new Vector3(camVec.x, 0f, camVec.y);
         gameObjectToRotate.LookAt(flipped);
     }
 }
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 aclee · Apr 09, 2015 at 05:31 PM 0
Share

for some reason your solution gave me a weird offset with the rotation so after i translated it to JS i change it to use vector 2 as the mouse position and middle of the screen and it worked.

 var gameObjectToRotate:Transform;
 private var middleOfScreen:Vector2;
 private var mousePos:Vector2;
 private var target:Vector2;
 private var flipped:Vector3;
 
 function Start () {
     middleOfScreen = new Vector2(Screen.width/2, Screen.height/2);
 
 }
 
 function Update () {
     mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
     target = mousePos - middleOfScreen;
     flipped = new Vector3(target.x, 0f, target.y);
     gameObjectToRotate.LookAt(flipped);
 }
avatar image Xarbrough · Apr 09, 2015 at 05:36 PM 0
Share

Weird, I did test my own code and it worked for me, so not sure what could have gone wrong; and I don't see a difference in functionality between your js version and $$anonymous$$e. But glad, that it worked in the end.

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

20 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

Related Questions

Fixed camera on rails. 2 Answers

Move character using Camera Input 2 Answers

Making my look script turn smoothly 0 Answers

Move RigidBody character relative to camera. 2 Answers

Simple way to make an object go up and down?(Y axis) 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