Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 BeltarPL · May 16, 2017 at 04:18 PM · rotationtargetmaxcannonfollowing

How to limit rotation to target

     if (m_LastKnownPosition != SelectedTarget.transform.position)
     {
         m_LastKnownPosition = SelectedTarget.transform.position;
         m_LookAtRotation = Quaternion.LookRotation(m_LastKnownPosition - transform.position);
     }
     if (transform.rotation != m_LookAtRotation)
     {
         transform.rotation = Quaternion.RotateTowards(transform.rotation, m_LookAtRotation, RotationSpeed * Time.deltaTime);
     }

This script rotates GameObject with it, to SelectedTarget. But I like to limit this rotation. So, if the SelectedTarget is behind this GameObject it won't rotate. (I like to get effect of cannon on the ship. Cannon won't to shoot by his ship. ;D)

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 toddisarockstar · May 18, 2017 at 12:20 AM

i assume the cannon is a child object of a ship that also rotates. So you would need to use local rotation of the cannon to check degrees. i also assume that this is 3D and Y is up. this would limit the local Y axis of the cannon on the ship when attached to the cannon by storing where it started in the start function.

     public float speed;
     public float max;
 
     float startrotation;
     Vector3 v;    
     float twist;
     void Start (){
         //store the cannons local start rotation on y axis;
         startrotation=transform.localEulerAngles.y;
 
         //set the amount of rotation degree allowed from the start rotation
         max = 30f;
     }
 void Update(){
 
         //change speed to your liking
         speed = Time.deltaTime*50f; 
 
           
             v = SelectedTarget.transform.position-transform.position;
             v = v.normalized;
             transform.rotation = Quaternion.LookRotation (Vector3.RotateTowards (transform.forward, v,speed, 0.0F));
     
         // get the difference between start angle y and current y
     twist = Mathf.DeltaAngle (startrotation, transform.localEulerAngles.y);
 
     if(twist>max){transform.localEulerAngles=new Vector3(transform.eulerAngles.x,max,transform.eulerAngles.z);}
     if(twist<-max){transform.localEulerAngles=new Vector3(transform.eulerAngles.x,-max,transform.eulerAngles.z);}
         
 
 }
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
avatar image
0

Answer by ImReaper · May 18, 2017 at 10:31 AM

You can lock the rotation of an object using Mathf.Clamp(current_value, min, max)

I would suggest you trying to put this line at the end of the update. I am assuming that it is the Y axis:

 transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Clamp(transform.eulerAngles.y, MIN_VALUE, MAX_VALUE), transform.eulerAngles.z);

In MIN_VALUE and MAX_VALUE it is up to you to set.

you could also try to do this:

 public float MIN_VALUE = 0, MAX_VALUE = 50;
 
 void LateUpdate(){
      transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Clamp(transform.eulerAngles.y, MIN_VALUE, MAX_VALUE), transform.eulerAngles.z);
 }

And don't forget to apply the script to the object that you want to lock its rotation.

I hope it helps!

-GR1M

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Lock rotation axis? 4 Answers

Head and body rotate threshold 1 Answer

Determining Look Rotation As > A Number? 1 Answer

Min, Max Rotation Help 2 Answers

How to stop a unit from rotation once it gets to designated position? 2 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