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 Marcusaralius76 · Feb 28, 2021 at 04:44 PM · physicsdotsrotatetowards

(DOTS) How to slowly rotate an object to face a point?

I'm currently learning ECS, and would like my objects to be able to rotate to face a point (IE: a turret turning to face the player). This is what I have currently:

A component that marks the entity as rotatable and stores related values:

 using Unity.Entities;
 using Unity.Mathematics;
 [GenerateAuthoringComponent]
 public struct Rotateable : IComponentData
 {
     public RotationType rotationType;
     public float rotationSpeed;
     public bool hasTarget;
     public quaternion rotationTarget;
 }

A system which either automatically snaps the entity to the new rotation or rotates slowly (enum is smooth or snap)

         Entities.ForEach((ref Rotation rot, in Rotateable rotateable) =>
         {
             var target = rotateable.rotationTarget;
             var speed = rotateable.rotationSpeed;
             var type = rotateable.rotationType;
 
             if (rot.Value.Equals(target)) return;

             if (type == RotationType.snapToPoint)
             {
                 //snap to rotation
                 rot.Value = target;
             }
             else
             {
                 if (!rot.Value.Equals(target))
                 {
                     //rotate at rotationSpeed to match needed angle
                     var delta = quaternion.RotateX(math.radians(speed * dt));
                     var step = math.mul(target, delta);
                     rot.Value = math.mul(step, rot.Value);// FIRST ATTEMPT (get errors)
                 }
             }
         }).Schedule();

And lastly is a temporary targeting system that generates a random target for rotatable (which I just realized is spelled wrong

 rng.NextInt();
         var rngTemp = rng;
         Entities.ForEach((ref Rotateable rot, in Translation trans) =>
         {
             var newTarget = new float3();
             newTarget.x = rngTemp.NextInt(5);
             newTarget.y = rngTemp.NextInt(5);
             newTarget.z = rngTemp.NextInt(5);
 
             if (!rot.hasTarget)
             {
                 rot.rotationTarget = quaternion.LookRotation(newTarget - trans.Value, new float3(0, 1, 0));
                 rot.hasTarget = true;
             }
         }
         ).Schedule();
         this.CompleteDependency();

I've mostly been experimenting to see what works, as I'm still new to ECS. I'd troubleshoot it myself, but I'm getting fun and descriptive errors like

"Invalid AABB a"

"Invalid AABB aabb"

"Assertion failed on expression: 'IsFinite(d)'"

I'm not afraid to admit I'm completely lost on this. My current best guess is it has something to do with either

A, the targeting system improperly making quaternions

B, the physics bodies spazzing out when I try to set Rotation.Value

C, my smooth rotation equation being wrong

Any help would be greatly appreciated!

EDIT: I've gone through some troubleshooting, and it appears the errors are from PhysicsBody spazzing out from changing Rotation.Value. I'm working on rotating my entities with the built in PhysicsVelocity Component

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
0

Answer by Marcusaralius76 · Mar 01, 2021 at 02:55 PM

I fixed it! Changing rotation.Value does cause the PhysicsBody to spaz out, so that was removed.

I wanted my units to move like turrets (lower body is stationary or moves and rotates about Y axis toward a point, upper body rotates about Y axis, weapon rotates about Z axis), so I replaced the target Rotation with a target Position the turret would face. Since everything rotates on one of two axis, I set up an enum to tell the difference between up-down and left-right rotation.

To find the up-down angle, I used

 math.atan2(pos.y - target.y, math.distance(pos.x - target.x, pos,z - target.z));

which I think works, but I'll need to test more once I have a proper targeting system in place.

to find my current up-down angle, I used

 QuaternionExtensions.ComputeZAngle(rot.Value);

I used The QuaternionExtensions class, which I had to copy from WAYNGROUP on github.

I interpolated the other direction the same way. I then applied rotation using the Physicsbody based on whether or not the angle of the target was larger or shorter than the current angle. It's a bit janky once it gets near the intended rotation value, but it works for now.

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 Marcusaralius76 · Mar 07, 2021 at 07:38 PM 1
Share

Note that this did not actually fix it. After some further digging, I found out the guys at Unity hid slerp (used for slow rotations) in math rather than in quaternion

so the solution was:

 rotation.Value = math.slerp(currentRotation, targetRotation, speed * dt);

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

190 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

Unity (ECS & DOTS) Entity Sinking 0 Answers

how to capsule cast from within Entities.ForEach job? ECS 1 Answer

2D 360 degress platformer example needed 0 Answers

Can not add PhysicsCollider at runtime Unity in ECS 1 Answer

Changing mesh collider's mesh cause objects go into mesh. 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