0
\$\begingroup\$

With the script I'm using, it would make whatever model it was attached to, follow the player when they were close enough. Even though I have NOT updated to the newer version of Unity, the script isn't working how it used to.

While it does make the model face towards the player, it no longer follows/chases then and I have a suspicion that it may be because I am using rigidbody for the player controls instead of character controller (due to Character Controller no longer working after the latest Unity, which I did NOT update to)

Below is a copy of the script I'm using, which is super simple:

using System.Collections; 
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    //The target player
    public Transform Player;
    //At what distance will the enemy walk towards the player?
    public float walkingDistance = 10.0f;
    //In what time will the enemy complete the journey between its position and the players position
    public float smoothTime = 10.0f;
    //Vector3 used to store the velocity of the enemy
    private Vector3 smoothVelocity = Vector3.zero;

    //Call every frame
    void Update()
    {
        //Look at the player
        transform.LookAt(Player);

        //Calculate distance between player
        float distance = Vector3.Distance(transform.position, Player.position);

        //If the distance is smaller than the walkingDistance
        if (distance < walkingDistance)
        {
            //Move the enemy towards the player with smoothdamp
            transform.position = Vector3.SmoothDamp(
                transform.position, 
                Player.position,
                ref smoothVelocity, 
                smoothTime);
        }
    }
}

Any ideas why the model isn't chasing the player? I keep changing the settings to see if it's anything silly like that, but no matter what I put it, or what I do, the model just wants to stare but not follow or chase the player :/

Here's a screenshot of the hierarchy:

[protected by copyright[1]

And the player model does have the player tag. No matter what values I put into the inspector for the FollowPlayer script, the model with the attached script will not move towards the player.

UPDATE: I figured out it was because the model with the script attached didn't have a rigidbody component. The problem now is, the model runs away from the player.

\$\endgroup\$
  • 1
    \$\begingroup\$ There's nothing obvious wrong with the code. Can you show us how you've configured your objects in the inspector/hierarchy? \$\endgroup\$ – DMGregory Aug 13 at 11:18
  • \$\begingroup\$ @DMGregory I've added a screenshot of Unity that should help. In an older EXE of the game, it works normally, but I'm really stumped as to why it's not chasing the player anymore (also sorry for the late reply, things have been very busy lately) \$\endgroup\$ – CraftyMaelyss Sep 1 at 6:49
  • \$\begingroup\$ I notice you have an animator on this object. Is it possible the animator is overriding the position changes your script is trying to apply? If you disable/remove it, do you observe any difference in behaviour? \$\endgroup\$ – DMGregory Sep 1 at 7:14
  • \$\begingroup\$ @DMGregory I figured out why the script wasn't working. I moved the model up in the air, off the ground to see if it was stuck in the floor, but notice it didn't fall, which means the script isn't working because I didn't add a rigidbody component to it. Now I just need to figure out why the model is running away from the player, instead of towards them X'D \$\endgroup\$ – CraftyMaelyss Sep 3 at 7:36
  • \$\begingroup\$ This script doesn't rely on a Rigidbody to move the object..If the object has a Rigidbody, then you should not be moving it with transform.LookAt / transform.position — you should move the Rigidbody instead using its methods. \$\endgroup\$ – DMGregory Sep 3 at 10:20
1
\$\begingroup\$

there are lots of thing that can make this happen. one thing i found in your code is that:

 float distance = Vector3.Distance(transform.position, Player.position);

        //If the distance is smaller than the walkingDistance
        if (distance < walkingDistance)
        {}

you calculate distance from player and say if its less than a threshold. if you want to stap in ha distance you have to change direction of > in that if condition.

standard way is to use Vector3.MoveToward() https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

rigidBody is nonSense in your case as you are moving it using Transform. so you can simply remove rigidBody.

\$\endgroup\$
0
\$\begingroup\$

I figured out why they're running away. Because this model is so far away from the player, I made a duplicate player model and disabled the first one.

After deleting the duplicate and using the original, it stopped the model from running away from the player.

\$\endgroup\$
  • \$\begingroup\$ In future, please be sure to include important details like that in your question. We won't be able to help you with aspects of your project that you haven't told us about. To make sure you're not leaving out important information, always try to reproduce the issue in a new empty project, using only the information you've shared in your question. If the problem goes away when you try to do this, then you know the cause is something outside what you've examined and described so far. \$\endgroup\$ – DMGregory Sep 13 at 16:29
  • \$\begingroup\$ Sorry, I didn't even think it was having an impact until I started to experiment with everything else in the scene to try and figure out why it was doing that. \$\endgroup\$ – CraftyMaelyss Sep 14 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.