Hello. My problem is that when I run my game and the monster who walks toward the player gets “too” close (which isn’t close enough for me) start ‘‘teleporting’’ around. I have already added a rigidbody and froze the Y axis… doesen’t seem to help… So it just like teleports around and goes through ground etc. And the player can’t rly get close to it…
And ghost should be able to touch the player. Here’s the code. Thank you for all the help in advance! ^ ^
#pragma strict
var player : GameObject;
var MovementSpeed:float;
function Start () {
}
function Update () {
transform.LookAt (player.transform);
transform.Translate (0,0,MovementSpeed*Time.deltaTime);
}
Try setting the rigidbody to isKinematic.
How about this (attach script to enemy)
Transform playerTransform;
float speed = 1;
float turnSpeed = 50;
void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
//rotate enemy facing player
Vector3 relativePos = playerTransform.position - transform.position;
Quaternion newRotation = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, Time.deltaTime * turnSpeed);
}
void FixedUpdate()
{
rigidbody.AddRelativeForce(Vector3.forward * speed, ForceMode.Impulse);
//limit max speed
if (rigidbody.velocity.magnitude > speed)
rigidbody.velocity = rigidbody.velocity.normalized * speed;
}