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
0
Question by OMAR3D · May 09, 2013 at 09:32 AM · camera-looklookatmouse

Camera look at problem

i'm trying to make the player and camera look at mouse position it works fine in y axis but in x it still moves in the y only , any help ? and there is another something when i hit play the camera rotated in a different way not the one it was set for ? my code

 function Update() 
 { 
  var target : Vector3 = (Input.mousePosition);
 
 transform.LookAt(target);
 }
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
1

Answer by robertbu · May 09, 2013 at 12:30 PM

This problem, in various forms, has been answered many times on this list. But most of the answers I found in a quick Google search were specialized. So here is a simple answer to what can be a complex problem. The reason your code is not working is that Input.mousePosition is in Screen coordinates and your transform lives in world coordinates. So you have to convert between the two.

 function Update () {
     var target = Vector3(Input.mousePosition.x, Input.mousePosition.y, 8.0);
     target = Camera.main.ScreenToWorldPoint(target); 
  
     transform.LookAt(target);
 }

See the "magic" 8.0? When using ScreenToWorldPoint(), the 'Z' parameter is the distance in front of the camera. Unity is 3D, so we need to specify not only a screen position to convert, but the distance in front of the camera from that position to make the conversion. Note the closer this point is to the character (i.e. the object this script is attached to), the stronger the LookAt() response.

For exmaple, imagine someone is moving a hand around on a large window. How much you move your head to follow the hand will depend on how close you are to the window. The same thing applies here. Play with different values for the 'Z' parameter and you can see how this applies to the LookAt code.

Note for your game, you will likely need to do something more complex with the 8.0. Instead of a hard-coded value, it will be some distance in front of the player, or perhaps some fraction of the distance between the camera and the player.

Comment
Add comment · Show 3 · 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 OMAR3D · May 09, 2013 at 01:12 PM 0
Share

Thanks for your answer i really appreciate it. but when i used it the camera kept moves like crazy?

avatar image robertbu · May 09, 2013 at 01:14 PM 0
Share

This script needs to be attached to the object you want to look at the mouse position. It should not be attached to the camera. To get you started:

  • Create a new scene

  • Put a cube in the sceen at (0,0,0).

  • Attach the script to the cube

  • Hit play

  • Repeat with different 'Z' values (try 9.5 ins$$anonymous$$d of 8.0 for example).

avatar image OMAR3D · May 09, 2013 at 02:47 PM 0
Share

it works fine thanks man

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

13 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

Related Questions

FPS Camera Collision 3 Answers

camera to turn 1 Answer

How can I construct a 4x4 rotation matrix to look in the direction of a Vector4? 0 Answers

Mac version mouse lock not working properly 1 Answer

Adding some recoil to the Camera (FPS) 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