Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jacob johnston · Jun 03, 2011 at 02:25 AM · mouselookatmouse position

troubles with look at mouse script

i have this script:

//attach this script to your camera and drag your character GameObject in the fpc slot in the inspector

private var worldPos : Vector3; private var mouseX : int; private var mouseY : int; private var cameraDif : int; var fpc : GameObject;

function Start () {

 //determines how far down the ScreenToWorldPoint is from the camera position
 //it's calculated [height of camera] - [height of pivot point of character]
 //this is to ensure the character only rotates (via LookAt) along rotation.x and doesn't look up or down
 cameraDif = camera.transform.position.y - fpc.transform.position.y;


}

function Update () {

 mouseX = Input.mousePosition.x;
 mouseZ = Input.mousePosition.y;


 //this takes your current camera and defines the world position where your mouse cursor is at the height of your character -->translates your onscreen position of mouse into world coordinates
 worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));

 fpc.transform.LookAt(worldPos);

}

it works well and looks at the mouse.... most of the time; when the mouse is below the front of the ship on the screen. what i am trying to say is that if the mouse goes above the ship on the screen it will not look at it directly. then the back of the ship looks at its exact opposite position. like if you cut the screen down the middle vertically it would take the mouse position and mirror it on the other side. any suggestion. all help is appreciated thank you

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by testure · Jun 03, 2011 at 02:40 AM

this is called gimbal lock, and why most freelook systems will limit X rotation in some way. Long story short, you don't want your camera's X rotation to go past 89 degrees in either direction or you'll be gimbal locked.

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 testure · Jun 03, 2011 at 02:43 AM 0
Share

also, if you don't know what gimbal lock is, or why it happens- here's a useful video explaining the euler gimbal lock phenomenon:

http://www.youtube.com/watch?v=zc8b2Jo7mno

avatar image
0

Answer by jacob johnston · Jun 03, 2011 at 02:57 AM

if this is so, do you have any suggestions on a fix to this problem or another link to fix the problem. this is the closest i have gotten to getting the correct effect i wanted in my game. thank you for any help

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 testure · Jun 03, 2011 at 03:03 AM 0
Share

like I said, you need to limit your x rotation to not go past 89 degrees in either direction.

after you take your mouse input and calculate your new rotation, clamp the rotation. IE: $$anonymous$$athf.Clamp(mouseX, -89, 89); // clamp mouseX between -89 and 89 degrees.

here's a video that essentially covers what you're trying to do: http://www.youtube.com/watch?v=ZggxVn93ePI

And lastly- make sure you post comments as comments, not as answers :)

avatar image jacob johnston · Jun 03, 2011 at 03:19 AM 0
Share

this is all turning out really confusing and hard. is there any way to write a script that just makes the game object face the mouse position along one plane so that it does not tilt up or down? also, to do this without getting the camera involved? something more simple. is this possible, thank you.

avatar image testure · Jun 03, 2011 at 04:51 AM 0
Share

Sorry man, I'm not sure how to help you... if you don't know how to edit the script you have, you may be in over your head at the moment. $$anonymous$$y suggestion would be to head over to 3DBuzz and look at their third person camera tutorial.. it's free, and it will explain a lot of the things you're having trouble with right now.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

transform.LookAt gives a twist 2 Answers

Object Look At Mouse 2 Answers

Neither CursorLockMode.Confined or SetResolution work 0 Answers

Lookat mouse, without raycasting? 1 Answer

Making character 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