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 Jagster · May 04, 2014 at 10:57 PM · c#camera rotaterotate objectclamped rotationz axis

Rotation around Z axis wont lock correctly (2D)

I've recently started work on a small space game and im having trouble getting the camera to stop rotation around the Z axis, by this i mean that although the camera does lock it still moves a few degrees out of place, the images below explain it better than i do (i hope)

this image shows what i want the game to look like when the player (spaceship) is rotating, you can see that the edge of the map is perfectly straight

alt text

however in its current state when i rotate the player around the Z axis it seem to lock it in the rotation seen in the image below, it wont move any further than this but it still causes the camera to move by a few degrees. I'm very new to unity so i have no real idea why this is happening or how i can fix it, any help would be appreciated and thanks in advance

alt text

i only have two scripts in place at the moment, this issue started when i added the locking script (second one)

this script is attached to the player object

 public float forwardSpeed = 10f;
 public float reverseSpeed = 5f;
 public float turnSpeed = 50f;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {

     if(Input.GetKey(KeyCode.W))
         transform.Translate(Vector2.up * forwardSpeed * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.S))
         transform.Translate(-Vector2.up * reverseSpeed * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.A))
         transform.Rotate(Vector3.forward, turnSpeed * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.D))
         transform.Rotate(-Vector3.forward, turnSpeed * Time.deltaTime);    


     Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

     Debug.Log (mouseWorldPosition);
 
     }

this script is attached to the main camera, which is a child of the player

 public bool PositionX = false;
 public bool PositionY = false;
 public bool PositionZ = false;

 public bool RotationX = false;
 public bool RotationY = false;
 public bool RotationZ = false;

 private Vector3 temp;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {

     if (PositionX == true) {
         temp.x = 0.0f;
         transform.position = temp;

     }

     if (PositionY == true) {
         temp.y = 0.0f;
         transform.position = temp;
         
     }

     if (PositionZ == true) {
         temp.z = 0.0f;
         transform.position = temp;
         
     }

     if (RotationX == true) {
         transform.rotation = Quaternion.Euler(0.0f, transform.rotation.y, transform.rotation.z);
         
     }
     
     if (RotationY == true) {
         transform.rotation = Quaternion.Euler(transform.rotation.x, 0.0f, transform.rotation.z);
         
     }
     
     if (RotationZ == true) {
         transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 0.0f);
         
     }    
 }





untitled-1.png (35.0 kB)
untitled-2.png (39.8 kB)
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
0

Answer by Pendrokar · May 06, 2014 at 02:05 PM

"this script is attached to the main camera, which is a child of the player"

Which means that when the player rotates so does the camera. If the camera isn't supposed to rotate with the player object, then the element that is rotating should be a child of the moving object and rotate that child instead.

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

21 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

Related Questions

really annoying rotation problem. 0 Answers

Rotating Produces Odd Results [Unity 2D] 1 Answer

How to account for situation using RotateAround 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


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