How do I make an enemy chase the player with a C script?
Basically I've been searching this for ages and the techniques and scripts I've seen and tried haven't been able to work, so this is why I'm asking here.
How do I write a script so that when the player walks within a certain range, the enemy will start to chase them but when they're further away, they will just look in their direction?
Again I've been through the answers, I've googled this topic and read through a lot of forums and I still can't seem to get it to work. This is one of the scripts that I tried to use from a tutorial(that didn't work) so maybe someone can see where I've gone wrong?
using UnityEngine; using System.Collections;
public class FogMan : MonoBehaviour { var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform; //current transform data of this enemy function Awake() { myTransform = transform; //cache transform data for easy acces/performance }
function Start()
{
Target = GameObject.FindWithTag("Basehuman01b").transform; //target the player
}
function Update () {
//rotate to look at the player
var distance = Vector3.Distance(myTransform.position, target.position);
if (distance<=range2 && distance>=range){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position -- myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop){
//move towards the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop) {
myTransform.rotation = Quaternion/Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
Answer by metalted · Sep 07, 2016 at 03:29 PM
I'm just going to approach this problem from scratch and try to meet the specs in as little code as possible. You ask for an enemy that will follow a player from a certain distance, and will always look at it.
C#:
//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);
}
}
Javascript:
//The target player
public var player : Transform;
//At what distance will the enemy walk towards the player?
public var walkingDistance : float = 10.0f;
//In what time will the enemy complete the journey between its position and the players position
public var smoothTime : float = 10.0f;
//Vector3 used to store the velocity of the enemy
private var smoothVelocity : Vector3 = Vector3.zero;
//Call every frame
function Update()
{
//Look at the player
transform.LookAt(player);
//Calculate distance between player
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, smoothVelocity, smoothTime);
}
}
@metalted How do I stop the enemy from going through solid objects? He's following them now, exactly how I wanted but he seems to phase right through physical objects(and I checked, they all have mesh colliders) so how do I stop him from doing that?
@metalted I applied this script to a small animated butterfly that I created but it goes straight to the player's feet when it's original position is a few feet off the ground, how do I fix this?
A possibility is to create a class variable to store the y position of your butterfly in.
Then in the start method, assign the y position to the variable or set the value manually if that is what you prefer.
If you look at the code, the part of the Vector3.SmoothDamp(...) will move the transform to the target position. Because we use a Vector3, this will affect the x y and z position. To keep the y position at a constant value, you need to alter the Vector3. The parts you need to paste in will look something like this:
float yPosition;
Void Start(){ yPosition = transform.position.y; }
Void Update{ ... Vector3 wantedPosition = new Vector3(player.position.x, yPosition, player.position.z);
//Now the transform will only move in the xz plane. Enter this into your SmoothDamp:
transform.position = Vector3.SmoothDamp(transform.position, wantedPosition,....);
}
You can do this with the x y and z coordinates to make sure that they dont change. If you only want to change the x position for example, you can just lock the y and z.
I've tried it but I'm getting errors, this is what my script looks like after trying to add in the additional code:
using UnityEngine; using System.Collections;
public class Butterflies : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
//The target player
public Transform Basehuman01b;
//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(Basehuman01b);
//Calculate distance between player
float distance = Vector3.Distance(transform.position, Basehuman01b.position);
//If the distance is smaller than the walkingDistance
if(distance < walkingDistance)
{
//$$anonymous$$ove the enemy towards the player with smoothdamp
transform.position = transform.position = Vector3.SmoothDamp(transform.position, wantedPosition,....);
float yPosition;
{ yPosition = transform.position.y; }
{ ... Vector3 wantedPosition = new Vector3(Basehuman01b.position.x, yPosition, Basehuman01b.position.z);
}
}
}
Have I just entered this new code in the wrong area of the script?
//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;
//Y position of the transform private float yPosition;
void Start(){ yPosition = transform.position.y; }
//Call every frame void Update() { //Look at the player transform.LookAt(player); //Calculate distance between player float distance = Vector3.Distance(transform.position, player.position);
//Calculate the new Vector3
Vector3 wantedPosition = new Vector3(player.position.x, yPosition, player.position.z);
//If the distance is smaller than the walkingDistance
if(distance < walkingDistance)
{
//$$anonymous$$ove the enemy towards the player with smoothdamp
transform.position = Vector3.SmoothDamp(transform.position, wantedPosition, ref smoothVelocity, smoothTime);
}
}
Sorry for the bad comment, im trying to help you as quickly as possible but it turns out i cant select the text and change it to code on my phone. So try to piece it together the right way. If its impossible for you, ill edit it at a later time.
Answer by bonez9oh5 · May 08, 2017 at 06:52 AM
@CraftyMaelyss Does your enemy have a collider and rigid body attached? that could be why its passing through objects.
@bonez9oh5 Well whenever I try to attach a rigidbody, it causes some (hilarious) glitches but it definitely has a collider.