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 18, 2011 at 09:49 PM · rotationspaceaiminggimbal-lock

gimble lock and rotation problem

i am in the progress of making a space game in unity. i have gotten quite a ways into it and have had few problems. my goal is to have the front of the ship always look or aim at where the user places his or her mouse and i want it to rotate along one axis. i have a script on my camera object that carries this function out but i know now that th ship will not rotate fully because of a gimble lock. i have been thinking about how to fix this and tried a few things but none have proved to fix the problem. i am very interested in finding a way to fix this. if anyone knows how to fix this or an alternative method to reach my goal i would greatly appreciate it. thank you so much

 /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;
 
     Mathf.Clamp(mouseX, -89, 89); 
     // clamp mouseX between -89 and 89 degrees.
     //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);
 
 }
Comment
Add comment · Show 7
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 Chris D · Jun 18, 2011 at 10:39 PM 1
Share

If you could post your current code, it would help.

avatar image jacob johnston · Jun 18, 2011 at 10:45 PM 0
Share

[code added to post]

avatar image Chris D · Jun 18, 2011 at 11:32 PM 0
Share

Ok, so you're locking when you do what?

avatar image jacob johnston · Jun 18, 2011 at 11:42 PM 0
Share

the spaceship which i want to rotate towards the mouse. only turns 180 degrees (not even that much). it faces downward (nose of ship pointing towards bottom of screen) and from there will only rotate 90 degrees to the left or right.

avatar image Chris D · Jun 18, 2011 at 11:56 PM 3
Share

One somewhat off-topic comment: would you please remember to approve the correct answer on the questions you ask? Looking through your previous entries, you haven't accepted a single one as a solution.

It will help people with similar problems find their solution faster if you acknowledge that someone's provided a solution to you.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Chris D · Jun 18, 2011 at 11:48 PM

It looks to me that you're clamping your mouse position between -89 and 89 pixels (as seen on screen). I'm assuming you wanted to clamp the angle it's facing between the same range instead?

Also,

 mouseZ = Input.mousePosition.y;

vs

 worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, **mouseY**, cameraDif));

you're assigning to mouseZ but trying to use mouseY.

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 aldonaletto · Jun 19, 2011 at 12:34 AM 2
Share

You can restrict the rotation to X axis fixing the Y coordinate of worldPos. If you do this:

   worldPos = camera.ScreenToWorldPoint(...
   worldPos.y = fpc.transform.position.y;
   fpc.transform.LookAt(worldPos);

your spaceship will be kept completely horizontal. If you want it to incline a little to the ground, just subtract some constant value from worldPos.y before fpc.transform.LookAt.
NOTE: as @ChrisD commented, please click the green button if his answer solve your problem!

avatar image
0

Answer by jacob johnston · Jun 19, 2011 at 12:42 AM

thank you so much that fix the problem completely. i have been working on this for a month or so now. thanks, if anyone wants the completed script that fixed the problem it is right here:

//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, mouseZ, cameraDif));
 worldPos.y = fpc.transform.position.y;
 fpc.transform.LookAt(worldPos);

}

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Limit local rotation around x, y, and z axes? 0 Answers

unwanted rotation on the y and z axes 1 Answer

IMU Sensor and Quaternion 2 Answers

Controlling an asteroid using a rigidbody. 1 Answer

Drift Rotation 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