Search Unity

How do I have an Entity "LookAt" a WorldSpace Point?

Discussion in 'Physics for ECS' started by goodnewsjimdotcom, Aug 31, 2021.

  1. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    In a SystemBase.ForEach, how do I "LookAt" a point?(aka rotate such that my game object faces this point.)

    I found clues here: https://forum.unity.com/threads/how...on-in-ecs-also-documentation-feedback.650656/


    • my code:
      Entities
      .WithName("RotationSpeedSystem_ForEach")
      .ForEach((ref Rotation rotation, ref Translation translation, ref LocalToWorld ltw, ref MovementDataEntity movementData, ref DroneDataEntity dd, ref PhysicsVelocity pv) =>
      {

      //TRYING for Entity to look at me. My location: 5,20,54



      UnityEngine.Vector3 newDir = UnityEngine.Vector3.RotateTowards(ltw.Forward, new UnityEngine.Vector3(5, 20, 54), 13f, 0f);


      rotation.Value = quaternion.EulerXYZ(newDir.x, newDir.y, newDir.z);

      my picture result: https://crystalfighter.com/code/16.png
     
  2. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    I have some cool videos of 50,000 drones being rendered, but I need to figure out how to have them face me, and how to thrust slowly in their forward facing vector:
    &


    If I could have em face the player and apply thrust, I'd have 50,000 drones chasing me...
     
  3. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    Solved:

    float3 pos;
    pos.x = playerPos.Value.x;
    pos.y = playerPos.Value.y;
    pos.z = playerPos.Value.z;


    float3 pos2;

    pos2.x = translation.Value.x;
    pos2.y = translation.Value.y;
    pos2.z = translation.Value.z;

    float3 relativePos = pos - pos2;

    // the second argument, upwards, defaults to Vector3.up

    quaternion start = rotation.Value;
    quaternion end = quaternion.LookRotation(relativePos, math.up());

    //quaternion result=quaternion.sl
    rotation.Value = end;



    Anyone know how to slerp in dots?
     
    AlexHolderDev likes this.
  4. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    math.slerp()
     
    Aethenosity likes this.
  5. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    Yup, Cookie Stealer, 100% right.

    Check out videos now: