• 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 AadyAhmed · May 26, 2019 at 02:17 PM · rotationrotate

How can I rotate towards a vector3 position in pure ecs?

Normally I do just use transform.LookAt, Vector3.RotateTowards or Quaternion.LookRotation. But I can't get any of them to works, so there a diffrent way of doing this?

Comment
Add comment · Show 2
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 highpockets · May 26, 2019 at 02:22 PM 0
Share

What exactly do you need to do? This is exactly what the functions you mention above do. Why is it not working this time?

avatar image AadyAhmed highpockets · May 26, 2019 at 02:58 PM 0
Share

I have a bunch of characters moving around and what I am trying to do is make them face the direction they are moving towards.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ruinerwarrior · Jul 10, 2020 at 07:09 PM

You can use the quaternion class of the Unity.Mathematics package, like so:

 quaternion.LookRotation(dir, new float3(0, 1, 0));
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 highpockets · May 26, 2019 at 03:12 PM

Does the below code not work?? If not please explain what is happening. This is based on your transform to always intend to keep the y axis towards its own up position which may or may not work depending on your situation, but if you can explain more about your issue, maybe it will become clear as to where you want the y axis of the transform to line up to.

 Vector3 direction = target.position -  transform.position;
 transform.rotation = Quaternion.LookRotation(direction, transform.up);
Comment
Add comment · Show 3 · 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 AadyAhmed · May 26, 2019 at 03:29 PM 0
Share

this is what I normally would do, but since I am doing it in ecs I don't have access to transforms. Here is my code, sorry if the problem is obvious.

 protected override void OnUpdate()
 {
     Entities.ForEach((ref Translation translation, ref Entity$$anonymous$$ovementComp movementComp, ref Rotation rot) => {

         Vector3 playerPos = GameObject.FindWithTag("Player").transform.position;

         if(Vector3.Distance(translation.Value,movementComp.dest) < 1)
         {
             movementComp.dest = new Vector3(UnityEngine.Random.Range(-40f,40f),-1, UnityEngine.Random.Range(-40f,40f));
         }

             if(translation.Value.x < movementComp.dest.x){
                 translation.Value.x += Time.deltaTime*movementComp.speed;
             }
             if(translation.Value.x > movementComp.dest.x){
                 translation.Value.x -= Time.deltaTime*movementComp.speed;
             }

             if(translation.Value.z < movementComp.dest.z){
                 translation.Value.z += Time.deltaTime*movementComp.speed;
             }
             if(translation.Value.z > movementComp.dest.z){
                 translation.Value.z -= Time.deltaTime*movementComp.speed;
             }

             if(translation.Value.y > movementComp.dest.y){
                 translation.Value.y -= Time.deltaTime*movementComp.speed;
             }

     });
 }
avatar image highpockets AadyAhmed · May 26, 2019 at 04:44 PM 0
Share

But you can’t include transform within the entity setup?? I’m not to familiar with ecs, have to take a deeper look. Do you know how to make a quaternion from scratch?? XYZ being the rotation axis and w being the angle in radians of the rotation and then normalize the quaternion when all said and done. I made a post here:

https://forum.unity.com/threads/quaternions.4836/#post-4426021

about how to construct a quaternion from scratch and once you make the method, just reuse it whenever you need it. Hopefully that helps

avatar image AadyAhmed · May 26, 2019 at 05:04 PM 0
Share

That would work, but I am trying to do it in pure ecs. And I don't have access to transforms. Have a look at my code, I am kind new to ecs so forgive me if it is obvious.

  protected override void OnUpdate()
 {
     Entities.ForEach((ref Translation translation, ref Entity$$anonymous$$ovementComp movementComp, ref Rotation rot) => {

         Vector3 playerPos = GameObject.FindWithTag("Player").transform.position;

         if(Vector3.Distance(translation.Value,movementComp.dest) < 1)
         {
             movementComp.dest = new Vector3(UnityEngine.Random.Range(-40f,40f),-1, UnityEngine.Random.Range(-40f,40f));
         }

             if(translation.Value.x < movementComp.dest.x){
                 translation.Value.x += Time.deltaTime*movementComp.speed;
             }
             if(translation.Value.x > movementComp.dest.x){
                 translation.Value.x -= Time.deltaTime*movementComp.speed;
             }

             if(translation.Value.z < movementComp.dest.z){
                 translation.Value.z += Time.deltaTime*movementComp.speed;
             }
             if(translation.Value.z > movementComp.dest.z){
                 translation.Value.z -= Time.deltaTime*movementComp.speed;
             }

             if(translation.Value.y > movementComp.dest.y){
                 translation.Value.y -= Time.deltaTime*movementComp.speed;
             }

     });
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

144 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

Related Questions

Make sphere rotate when controlled 7 Answers

Strange rotation pattern. 0 Answers

different axis for different objects? 1 Answer

Rotating wrong 0 Answers

How can I rotate an object without moving it up or down? 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