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 /
This question was closed Jul 08, 2014 at 06:50 PM by pheash for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by pheash · Jun 10, 2012 at 09:11 PM · rotationangletower-defense

Rotation Angle across 0, opposing angle?

Hi I ve got a tower that rotates towards a target. I am using a helper object that looks at the target and then i am lerping from the current rotation to the helper objects rotation. the problem that i have atm is when the target passes infront of my tower it rotates all the way around, so instead of going from +5 to -5 degrees (i want it to rotate 10 degrees) it rotates 350 degrees. what am i missing? here is a bit of my aiming code:

 Aiming(){
         rotateHelper.transform.LookAt(target.transform.position);
         rotateHelper.transform.localEulerAngles.x=0;
         rotateHelper.transform.localEulerAngles.z=0;
         endBaseAngle = rotateHelper.transform.localEulerAngles.y; 
          baseAngleDifference = Mathf.Abs(endBaseAngle-startBaseAngle);
         if(baseAngleDifference>180){           // this bit is trying to change that angle but fails
         baseAngleDifference = Mathf.Abs(endBaseAngle-startBaseAngle);
         print("new base angleDiff = "+baseAngleDifference);
     }
     changeBase=true;
 }
 // gets called in update if changeBase is true..
     function ChangeBase(){            //small rotations should take less time. compare start and emd angle before we go here and somehow multiply rotate speed
         soundPlayer.PlaySound(soundBaseRotate,0.0);
         weaponBase.transform.localEulerAngles.y = Mathf.Lerp(startBaseAngle,endBaseAngle,rotateTimer*baseRotateSpeed);
         weaponBase.transform.localEulerAngles.x=0;
         weaponBase.transform.localEulerAngles.z=0;
         rotateTimer+=Time.deltaTime/baseAngleDifference;    //dividing by difference of old and new angle so a small angle is reached faster
         if(rotateTimer*baseRotateSpeed>=1.0){
             rotateTimer=0.0;
             changeBase=false;
         }
     }

Also if you have any other improvements to my coding style it would be appreciated since i am still quite new to programming. I am sure there is an easier way to do this.

thanks in advance

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

  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jun 10, 2012 at 10:05 PM

localEulerAngles (as well as eulerAngles) return an angle usually between 0 and 360: when the angle falls below 0, it usually becomes 359, not -1 as you could expect.
To avoid this problem (among several others...), it's better to do it with quaternions:

var speed: float = 10; // speed in degrees per second

function Update(){ // NOTE: rotateHelper isn't necessary to this code rotateHelper.transform.LookAt(target.transform); // find the target direction relative to weaponBase: var dir = target.transform.position - weaponBase.transform.position; // kill any height difference to make the direction strictly horizontal: dir.y = 0; // calculate the desired quaternion: var rot = Quaternion.LookRotation(dir); // weaponBase rotates to target at constant speed: weaponBase.transform.rotation = Quaternion.RotateTowards(weaponBase.transform.rotation, rot, speed * Time.deltaTime); } This code will make weaponBase rotate horizontally at speed degrees per second in the target direction, independently of rotateHelper (remove it if it's unnecessary).

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 pheash · Jul 27, 2012 at 08:50 AM 0
Share

thanks for the quick reply Aldo. Rotate towards is what i forgot about, i think this will work perfectly. Also for ppl having a similar issue I ve read up on vectors here: http://unity3d.com/support/documentation/$$anonymous$$anual/DirectionDistanceFromOneObjectToAnother.html The last time i ve done maths was in school and tbh ive forgotten loads so the manual provides a great starting point to read up on things.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotation circle to stop at a certain angle 1 Answer

Angle to Rotation 2 Answers

0-360 Y degree from Vector3.Angle 1 Answer

Issue finding an angle with trigonometry 1 Answer

2D Torque rotation - stop at certain angle 0 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