Hello,
I’ve made a search in Unity Answers and I didn’t find what I wanted, or if I finded, it didn’t worked.
I want to make the enemy to chase the player, but I don’t know how to do that.
Thank you.
Hello,
I’ve made a search in Unity Answers and I didn’t find what I wanted, or if I finded, it didn’t worked.
I want to make the enemy to chase the player, but I don’t know how to do that.
Thank you.
I translated the best answer to C# for all of you C# users out there.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AIController : MonoBehaviour
{
public Transform Player;
int MoveSpeed = 4;
int MaxDist = 10;
int MinDist = 5;
void Start()
{
}
void Update()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}
}
}
}
Hey, I'm doing a 2D game for my VCE Game Making class, I've added this script into my unity and attached it to my enemy, but when i set it to follow my main character it doesn't follow him and my main guy is right in front of him. Any help?
– ric0030Never mind I figured out that you need to drag the object into the transform slot but it is just falling off my screen and going invisible
– unity_wIfM9PlhP0UL9Avar Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;
function Start ()
{
}
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}
}
}
Attach This Script to your Enemy use MinDist for enemy to stop at certain range and MaxDist For shooting at your player if you have anything like that :) Hope This Helps You :D
– Dee_VaHi, I'm trying to use this script as well however I want the level to reload when the enemies get close enough to the player. How would you suggest doing this? I've been trying to add a collider and trigger to the script but it hasn't been working. Thanks!
– conversestar107Nevermind, I was able to figure out but thanks again for the help on the scripting for the enemies!
– conversestar107My version of ChristianBlandford’s script, with this the enemies will only look at the player when they are in range, move if they are in range but not further than stop.
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float=10f; //Range within target will be detected
var stop : float=0;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
target = GameObject.FindWithTag("Player").transform; //target the player
myTransform = transform; //cache transform data for easy access/preformance
}
function Update () { //rotate to look at the player
var distance = Vector3.Distance(myTransform.position, target.position);
if (distance<=range){
//look
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move
if(distance>stop){
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
Simplest AI possible, the zombie :
To expand that, you’ll need to make the AI walk idly until the player is close enough, and again when the player is too far / behind an obstacle. After that, pathfinding.
hi I'm new to unity i tried running using the script ,it works but the enemy character seems to be rotating continuously
– Itachi_UchihaHeres Mine:
var target : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I translated the code to C# since JavaScript is no longer supported in Unity.
public class AIScript : MonoBehaviour
{
public Transform Player;
int MoveSpeed = 4;
int MaxDist = 10;
int MinDist = 5;
public void Start()
{
}
public void Update()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
//Like anything?
}
}
}
}
There's another way to see the target position without using the lookAt? I'm trying to use that for unity 2D and it's not working at all..
– rpamaral@rpamaral - please don't post new questions as 'Answers' to old questions. First an 'Answer' is a solution on UA. Second, since this is really a new question, we ask that you open a new question. As it happens, this questions has been asked and answered a number of times since Unity release their new 2D stuff. Here is one answer: [http://answers.unity3d.com/questions/603757/2d-mouse-aiming.html][1] [1]: http://answers.unity3d.com/questions/603757/2d-mouse-aiming.html
– robertbumay i ask how you would add the attacking part for the zombies?
– RealSoftGamesHere is a good plugin, it may help you. https://www.assetstore.unity3d.com/#!/content/87744?aid=1101l34jr
– DeveshPandey