- Home /
can not move my enemy with rigidbody
I need my enemy to target the player and move towards the player and attack without moving through walls. My script used to work(everything except moving through walls) but I changed it to try to make the enemy not move through walls and now the enemy won't move at all. I attached a rigidbody, a character motor, a box collider, and a character controller. Heres my EnemyAI script.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
public int maxDistance;
// Use this for initialization
void Awake () {
myTransform = transform;
}
void Start(){
GameObject go = GameObject.FindGameObjectWithTag("player");
target = go.transform;
maxDistance = 2;
}
// Update is called once per frame
void FixedUpdate () {
myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
rigidbody.velocity = transform.TransformDirection(new Vector3(0,0, moveSpeed));
}
}
}
Answer by Slobdell · Jun 21, 2013 at 10:51 PM
Where is movespeed defined, you could be setting velocity to zero on all three axis? Also try not moving objects using their velocity. For rigidbodies you can apply force, or in this case since it should be a constant speed you can move them by lerping their transform's position.
As for going through walls you will need a collider on the wall as well.
there is a box collider on the wall that's not set to trigger so don't think that's the problem
Your answer
Follow this Question
Related Questions
Stopping my player from moving when hitting a wall. 5 Answers
How to properly move a rigidbody/collider? 2 Answers
rigidbody It's going up and bounces when is moving 1 Answer
How to tag? 1 Answer
[SOLVED] Object keeps sliding? 2 Answers