My code for a following enemy script
using UnityEngine;
using System.Collection;
public clas LookAtPlayer : MonoBehaviour {
var target = Transform; //current transorm data of this enemy
var moveSpeed = 1; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform = Transform; //current transform data of this enemy
}
function Update
//rotate to look at the player
//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;
}
What is the error and where is it?
You have missing brackets, missing parentheses, spelling errors, and you are are also mixing two languages (Javascript and C#). Rather than point out all of the mistakes, it’s easier for me to just give you the corrected code and show you how it should look.
Attach this script to your enemy gameobject, set the player gameobject to the target variable, then the enemy will follow the player.
C#
using UnityEngine;
using System.Collections;
public class LookAtPlayer : MonoBehaviour
{
public Transform target; //current transorm data of the player
public float moveSpeed = 1; //move speed
public float rotationSpeed = 3; //speed of turning
void Update()
{
//rotate to look at the player
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed*Time.deltaTime);
//move towards the player
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}