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 LeftyTwoGuns · Aug 21, 2013 at 10:35 PM · rotateclamplookrotation

Clamping LookRotation angle

I have a turret that aims up and rotates on the Z-axis, using LookRotation to look at specific game objects. I tried to use my eulerAngles clamp that works for my player controlled turret that rotates on the same axis with mouse input using transform.Rotate. Here is the script:

                Vector3 look = target.transform.position - transform.position;
                look.z = 0;
                targetRotation = Quaternion.LookRotation (look);
                transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
             
             transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp (transform.eulerAngles.z, CminAngle, CmaxAngle));

The Mathf.Clamp line just causes the turret to turn sideways and stay that way. Is clamping LookRotation not the same as clamping transform.Rotate?

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
3
Best Answer

Answer by robertbu · Aug 21, 2013 at 10:49 PM

Any eulerAngle you get is converted from a Quaternion. Any angle you set is converted to a Quaternion. The angle you set is not necessarily the same one you get back. For example execute this code:

 transform.eulerAngles = new Vector3(180,0,0);
 Debug.Log(transform.eulerAngles);

The value you will get back is (0,180,180), the same physical rotation, but a different Euler angle representation. So clamping your Euler angles will not work all the time. One way to fix your code is to instead test the angle of two Quaternions. Assuming your turret is pointed half way between the two max angles on start, you can do this (untested):

 using UnityEngine;
 using System.Collections;
  
 public class Tracker : MonoBehaviour { 
     
     public Transform target;
     public float maxAngle = 35.0f;
     private Quaternion baseRotation;
     private Quaternion targetRotation;
 
     
     void Start() {
         baseRotation = transform.rotation;
     }
     
     void Update() {
         
         Vector3 look = target.transform.position - transform.position;
         look.z = 0;
         
         Quaternion q = Quaternion.LookRotation (look);
         if (Quaternion.Angle (q, baseRotation) <= maxAngle)
             targetRotation = q;
         
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
     }
 }

Comment
Add comment · Show 5 · 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 LeftyTwoGuns · Aug 22, 2013 at 12:09 AM 0
Share

That seems to limit rotation to just one half of the turrets angle and also seems to make its movement a bit jittery and less smooth.

avatar image robertbu · Aug 22, 2013 at 12:37 AM 0
Share

I changed the code a bit. The maxAngle represents the maximum degrees from the base angle allowed. The old code would stop tracking immediately when the target left the field of view. This new code will continue to the limit. This means that if the enemy comes back from the same side, this code will pick them up sooner.

avatar image LeftyTwoGuns · Aug 22, 2013 at 01:18 AM 0
Share

That seems to work. But the best results are when I set the $$anonymous$$axRotation to 180. Does that mean the script is telling the turret it's allowed to rotate 180 degrees from the baseRotation?

avatar image robertbu · Aug 22, 2013 at 02:01 AM 0
Share

Yes. Setting it to 180 will allow the turret a full 360 degrees of rotation.

avatar image LeftyTwoGuns · Aug 22, 2013 at 02:59 AM 0
Share

Ok, now it makes sense. Thanks again!

avatar image
0

Answer by drudiverse · Apr 06, 2014 at 04:54 AM

You can use Vector3.Angle as well to monitor deviation from wished direction.

if(vector3.Angle(centrallook.vector,currentlook.vector)<45){rotate towards....);

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

17 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

Related Questions

How can I rotate my player head to face a target but only when the target is in the front of the player ? 1 Answer

Camera rotation around player while following. 6 Answers

Rotating to -forward goes upside down 1 Answer

Applying a clamp to Quaternion.LookRotation 1 Answer

Clamping the Camera, according to the player ? 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