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 SirChockemBerry · Jun 07, 2015 at 07:46 PM · rotationphysicsparentnegativezero

Turret Limitations on a Moving Object

I'm attempting to create a turret that follows it's parent spaceship, rotates towards the mouse, and has limitations on where it can rotate to. The problem occurs when attempting to clamp the turret to it's limitations. The clamp refuses to go beyond a certain point. alt text

The circle is where the turret starts off, the blue lines represent the rotation limits that happen at the two points. The original point works perfectly, and rotates towards the mouse inside of the clamping region. However, if I rotate the ship by say 180 degrees, and the turret ends up at the top point, it gets stuck and goes back to it's original clamp position when it reaches half way. The rotation in the inspector claims that it only goes up to 180 before resetting.

Here is my code for the turret:

 public float speed;
     public Vector3 rotate;
     public GameObject parent;
     
     Vector3 pos;
     
     void Update(){
         Vector3 mousePos = Input.mousePosition;
         pos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.transform.position.y - transform.position.y));
     }
     
     void FixedUpdate(){
         PlayerMove p = parent.GetComponent<PlayerMove>();
         Quaternion targetRot = Quaternion.LookRotation(pos - new Vector3(transform.position.x,0,transform.position.z));
         Vector3 rotate = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRot.eulerAngles.y, speed * Time.deltaTime);
         rotate.y = Mathf.Clamp(rotate.y, p.currentDir + 90, p.currentDir + 270);
         transform.eulerAngles = rotate;
     }


Using localRotation causes it to spaz out at any angle other than the original. I have been tampering with this for multiple hours now, and have managed to pull of my hair out, had to resort to asking for help before tearing skin apart as well.

Any and all help will be greatly appreciated.

EDIT: I have now identified the problem, after x amount of time passing, I've learnt that the problem happens whenever the angle goes past zero. For some odd reason, the turret refuse to go through zero without going the other way, causing it to go back to it's other limit. While I thought that the Vector3 just has problems with going through zero (which happened in another project) I attempted to use a different means of rotation which worked on the other project, here is the new code:

     public float speed;
     public float min;
     public float max;
     public Vector3 rotate;
     public PlayerMove p;
     public Quaternion targetRot;
     
     Vector3 pos;
     float r;
     
     void Start(){
         p = GetComponentInParent<PlayerMove>();
     }
     
     void Update(){
         Vector3 mousePos = Input.mousePosition;
         pos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.transform.position.y - transform.position.y));
         
         min = p.currentDir + 90;
         max = p.currentDir + 270;
     }
     
     void FixedUpdate(){
         targetRot = Quaternion.LookRotation(pos - new Vector3(transform.position.x,0,transform.position.z));
         r = targetRot.eulerAngles.y;
         r = Mathf.Clamp(r, min, max);
         
         transform.rotation = Quaternion.Euler(0, r, 0);
         //rotate = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, r, speed * Time.deltaTime);
         //transform.eulerAngles = rotate;
     }

I used Quaternion.Euler with the individual variables rather than the Vector3, as this seemed to work the last time I encountered this problem.

EDIT2: I've learned that it isn't just when going through zero, it's whenever the min or max end up in the negatives as the ship continuously rotates. The clamp borrows the variables from the ship rotation, which is a float that increases and decreases as the ship rotates. There isn't a clamp on that, since that restricts the ship rotation.

Still hoping for an answer...

bug-poop.png (42.3 kB)
Comment
Add comment · Show 2
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 Ricewind1 · Jun 08, 2015 at 12:52 AM 0
Share

I've had problems with this as well and never quite fixed it. If the turret only has to move along 1 axis, freeze the others on the rigidbody. Then, in the late update, you could try adding an if-statement that only allows movement if the rotate.y is between certain angles.

avatar image SirChockemBerry · Jun 08, 2015 at 09:19 AM 0
Share

@Ricewind | It doesn't use the Rigidbody in any of the objects, and the rotation is only changed along the y axis. Before the turret starts moving, the possible angles are clamped to the limits, meaning that the turret was never able to rotate past those angles before movement started. It works fine in the original angle, just not at any other angle.

0 Replies

· Add your reply
  • Sort: 

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

Moving child transform also moves parent rigidbody 0 Answers

Separate child rotation 1 Answer

How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer

Keep rotation when changing parent in Unity 3D 1 Answer

Bike stay upright based on surface normal 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