Search Unity

Resolved make Entity follow Player (GameObject)

Discussion in 'Entity Component System' started by JediNizar, May 30, 2021.

  1. JediNizar

    JediNizar

    Joined:
    Nov 13, 2016
    Posts:
    111
    Hi can some1 help me to make an Entity follow a Player GameObject?
    Actually I need the Entity with an offset of the player movement direction, that means if the player goes forward the entity would be a little bit ahead of the player.
    If the player moves backward the entity would be a bit behind etc..
    here is what I tried

    Code (CSharp):
    1.  Entities.WithAll<Tag_Player>().ForEach((ref Translation translation) =>
    2.         {          
    3.             translation.Value = (SubSceneReferences.Instance.Player.transform.right * 5f +
    4.             SubSceneReferences.Instance.Player.transform.position;
    5.         });
    that way the entity is always ahead the player facing direction.

    any suggestions?
     
  2. mikaelK

    mikaelK

    Joined:
    Oct 2, 2013
    Posts:
    284
    This solution you have, the entity is 5 units on the right side of the player?

    I recommend trying vector mathematics.
    You will probably need the normalized direction of the player in world space and also the forward speed.
    After that it should be just playing with the vectors.

    If the speed is positive start incrementing value starting from zero to the forward vector, clamp it and add that to that what you have. Assuming what you have is working as I suspect
     
  3. JediNizar

    JediNizar

    Joined:
    Nov 13, 2016
    Posts:
    111
    thank you, with your help and some1 else I got it

    Code (CSharp):
    1. if(SubSceneReferences.Instance.EntityPlayerPreviousPostion != SubSceneReferences.Instance.controller.transform.position)
    2. {
    3.      direction = (SubSceneReferences.Instance.controller.transform.position - SubSceneReferences.Instance.EntityPlayerPreviousPostion).normalized;
    4.       translation.Value = (direction * SubSceneReferences.Instance.EntityCollisionDistance) + SubSceneReferences.Instance.Player.transform.position;
    5. }