- Home /
Used another aproach
Ai script(rotation) causes game to crash
Hey,
for my first attemps lerning unity and building my first game, Im using the script bellow. For some reason the game crashes once the ai gets closer to my player. I found out removing the "transform.Rotate" line(19) prevents the game from crashing. But of course the script isnt doing its job anymore once i removed this(ai keeps cricling, looks funny but its not what I want). Ive already spent about an hour trying to solve the problem but I cant find it... Maybe somone of you nice guys can help me with this
using UnityEngine;
using System.Collections;
public class EnemyController : MonoBehaviour {
public Transform target;//set target from inspector instead of looking in Update
public float speed = 3f;
void Start () {
}
void Update(){
//rotate to look at the player
transform.LookAt(target.position);
transform.Rotate(new Vector3(0,-90,0),Space.Self);//correcting the original rotation
//move towards the player
if (Vector3.Distance(transform.position,target.position)>1f){//move if distance from target is greater than 1
transform.Translate(new Vector3(speed* Time.deltaTime,0,0) );
}
}
}
Answer by M-Hanssen · Apr 25, 2016 at 08:38 AM
There is nothing wrong with your script. Works fine on my end.
A few questions:
What version of Unity do you use?
Is it your intention to move the object sideways?
Hey,
can't tell you from here the exact version(im not at home), but it should be the latest stable one(downloaded like 3 days ago). $$anonymous$$y intention is to move the enemy object straight to the (moving) player object.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Enemy pick and throw an object 1 Answer
AI scripting Question. 2 Answers
Can't properly duplicate an enemy AI 0 Answers