Rabbit Follow Selected Player

Options

Greetings, everyone. I am facing a problem where my rabbit isn't following the player who collides with it. How can I do this? Here is the code, just in case:


private bool ateBerry;   private Rigidbody rb;     void Start()   {     rb = GetComponent<Rigidbody>();     ateBerry = false;   }   void FixedUpdate()   {     RaycastHit hit;     if (Physics.Raycast(transform.position, Vector3.down, out hit))     {       Vector3 crossVector = Vector3.Cross(transform.right, hit.normal);       transform.rotation = Quaternion.LookRotation(crossVector, hit.normal);             if (!GameObject.FindGameObjectWithTag("RabbitOwner")) { return; }       transform.LookAt(GameObject.FindGameObjectWithTag("RabbitOwner").transform);             if (Vector3.Distance(transform.position, GameObject.FindGameObjectWithTag("RabbitOwner").transform.position) > 3 && ateBerry)       {         transform.position = Vector3.MoveTowards(transform.position, new Vector3(GameObject.FindGameObjectWithTag("RabbitOwner").transform.position.x, transform.position.y, GameObject.FindGameObjectWithTag("RabbitOwner").transform.position.z), Time.deltaTime * 3);       }     }   }     void OnCollisionEnter(Collision other)   {     if (other.collider.tag == "Player")     {       other.collider.tag = "RabbitOwner";       ateBerry = true;     }   }

Answers

  • Hello? Anybody here?

  • Klover
    Options

    Hello yes I'm here hold on a sec lol

  • Klover
    Options

    bool followingOther;


    void Update()

    {

    //We're just checking if this bool is true so we can know to follow the gameObject

    if(followingOther)

    {

    transform.position = otherGameObject.transform.position + new Vector3(0, 0, -1);

    }

    }



    void OnCollisionEnter (Collision otherGameObject)

    {

    //When we collide with the gameObject we want to follow, we set the bool to true so we can follow the player in Update()

    if(otherGameObject.gameObject.tag == "Player") //Or however you identify the players

    {

    followingOther = true;

    }

    }



    Let me know if you have any more questions at kleemoffdeveloper@gmail.com