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 boddole · Mar 03, 2015 at 02:01 PM · c#rotationvector3rotateangle

Trouble with Vector3.Angle() and RotateAround()

Hello everyone, I'm scripting a "turret" that looks at a target position (ignoring the Y difference between the two if any).

Unfortunately I've become stuck on a problem where I can never (on purpose) get Vector3.Angle() to be 0. At this point, the value will become close to 0 (say 0.03) then start flipping back and forth between the positive and negative value of that small number) which you can see in the screenshot. Occasionally, the value does become 0, but I don't know why.

I thought that the way I have the relevant part of this script would handle this (in that the desired rotation amount, at a small value, becomes the remainder of the current rotation to 0, meaning the angle should be 0 on the next frame), but obviously it does not.

                 //check if the Turret has a clear los according to whatever its impassible layers are:
                 Debug.DrawLine(transform.position, currentTargetPosition, Color.red);
                 if(!Physics.Linecast(transform.position, currentTargetPosition, impassibleLayer) ) //if raycast dosen't hit anything...
                 {
                     if(turretCentered) //update Turret so it knows it is not centered anymore
                     {
                         turretCentered = false;
                     }
                     
                     //Turret does not care about any Y position differences...
                     Vector3 currCustom = currentTargetPosition;
                     currCustom.y = transform.position.y; //ignore Y differences...
                     
                     Vector3 directionVector = currCustom - transform.position;
                     
                     float angle = Vector3.Angle(transform.forward, directionVector);
                     Debug.Log("angle is: " + angle);
                     
                     float rotAmount = maxRotationSpeed * Time.deltaTime;
                     Debug.Log("initial rotAmount is: " + rotAmount);
                     
                     if(Mathf.Abs(angle) - Mathf.Abs(rotAmount) <= 0f) //don't overshoot desired angle (which is 0)...
                     {
                         //Debug.Log("RotateTurret() breaking...");
                         //Debug.Break();
                         
                         rotAmount = angle; //set rotation amount to the remaining angle...
                         Debug.Log("rotAmount = angle: " + rotAmount);
                     }
                     
                     //now figure out if turret should rotate to the left or right:
                     if(Vector3.Cross(transform.forward, directionVector).y < 0f)
                     {
                         //Debug.Log("cross.y is <= 0f");
                         rotAmount = -rotAmount;
                     }
                     else
                     {
                         //Debug.Log("cross.y is > 0f");
                     }
                     
                     Debug.Log("final rotAmount is: " + rotAmount);
                     transform.RotateAround(transform.position, transform.up, rotAmount);
                     //


Any ideas on what is happening is appreciated, thank you for reading.

alt text

example.png (76.5 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
1

Answer by Owen-Reynolds · Mar 03, 2015 at 09:06 PM

I just skimmed the first part and glanced at the script, but: Unity has RotateTowards (and MoveTowards) for just such overshoot problems. Both are written so they will never overshoot, and do nothing if you are at the target.

They aren't complicated, and easy to write yourself (it looks like you did?) but they make code a little nicer. I suspect your overshoot math may have a problem.

RotateTowards always make the one most direct rotation -- it picks the rotation axis itself. Which is often what you really wanted. But, seems you could use MoveTowards directly on your angle var.

Comment
Add comment · Show 2 · 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 boddole · Mar 04, 2015 at 03:44 AM 0
Share

@Owen Reynolds:

"RotateTowards"

  • In this case, I'm not sure how to use it because it wants 2 Quaternions. I've got one (the turret transform), but I'm not sure how to get the other (since I'm trying to align to a Vec3 position, not another rotation.

"you could use $$anonymous$$oveTowards directly on your angle var"

  • I'm just not quite sure what you mean there.

Anyway, thank you for the response. It also tried LookAt() just to force a perfect direction, and sometimes angle is still not 0....

avatar image Owen-Reynolds · Mar 04, 2015 at 03:15 PM 0
Share

Can look all these things up, if you want to, such as "unity convert direction to quaternion." But takes time to learn.

LookAt is a different issue. If it gives you 0.001 for an angle, it's probably correct (and you are a little off-center.) But at least LookAt won't go backNforth if you keep calling it.

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

Flip over an object (smooth transition) 3 Answers

Having Trouble with Vector3.Angle 1 Answer

Rotating without gimbal lock 1 Answer

Rotating Joystick on Touch 0 Answers

How to rotate an object without using a timer 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