So in my game, the enemy will chase the player if he enters it’s raycast. The enemy detects that he is in the raycast, but won’t move! I can’t make the player var into a transform because it is supposed to represent the player (duh) and it won’t assign it in a prefab because it’s a game object!
It is 2D (so I can’t use LookAt) and I’m using Javascript.
#pragma strict
var health : float = 40;
var player : GameObject;//the player game object
var speed = 0.1; //houw fast it will go
var myLayerMask : LayerMask;
var foundHit : boolean = false; //detects if the player enters the raycast.
function Start() {
}
function Update(){
if(health <= 0) { //die
Destroy(gameObject);
}
print("enemy "+ health);
//if (move == true) {
//transform.position.x += this.speed;
//}
foundHit = false;// called every frame to reset found hit unless the player is still in the raycast
var hit : RaycastHit2D;
foundHit = false;
foundHit = Physics2D.CircleCast(transform.position, 5,Vector2.zero,5,myLayerMask); //raycast
if(foundHit == true) {
//if the player is in the raycast, it should start chasing the player
transform.position = Vector2.Lerp(transform.position, player.transform.position, Time.deltaTime);
//transform.position = Vector2.Lerp(transform.position, player.transform.position, speed);
print("hit");
}
}
Take the time to properly indent your code. No one wants to read code that’s all messy like that. It also makes spotting errors in your syntax or formatting more apparent to you as you’re working.
I agree 100% with @Schneider21 . What I can tell from your code is that you’re not even close to using Lerp correctly, which is a likely cause of your issue. Read this article for more information: How to Lerp like a pro | Chico Unity3D
my lerp worked just fine before. I just had to change the player transform to player.transform.position now it doesn’t work. This article isn’t helping me either. I just need it to simply move to the player over time.
Dont use lerp with time.deltatime. This is a common mistake. Lerp takes a value between 0 - 1 reprsenting the percetange between the values. Thus Lerp(a, b, 0) will return a while Lerp(a,b, 1) will return b. You need to increment
Ok, so apparently transforms decide to work now. But now I’m getting this error message
(28,34): BCE0017: The best overload for the method ‘UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)’ is not compatible with the argument list ‘(UnityEngine.Vector3, UnityEngine.Transform, float)’.
Here is my new code
#pragma strict
var health : float = 40;
var player : GameObject;//the player game object
var playerPos : Transform; //the transform of the player
var speed = 0.1; //houw fast it will go
var myLayerMask : LayerMask;
var foundHit : boolean = false; //detects if the player enters the raycast.
function Start() {
}
function Update(){
if(health <= 0) { //die
Destroy(gameObject);
}
print("enemy "+ health);
//if (move == true) {
//transform.position.x += this.speed;
//}
foundHit = false;// called every frame to reset found hit unless the player is still in the raycast
var hit : RaycastHit2D;
foundHit = false;
foundHit = Physics2D.CircleCast(transform.position, 5,Vector2.zero,5,myLayerMask); //raycast
if(foundHit == true) {
//if the player is in the raycast, it should start chasing the player
transform.position = Vector2.Lerp(transform.position, playerPos, speed);
print("hit");
}
}