WillB
1
Scripting noob here. I have a game populated with large amounts of enemies, so I decided to switch them from CharacterControllers to kinematic rigidbodies to optimize, since I’ve read on here that there’s much less overhead when doing so.
What I’m trying to do is very simple. I need to have my AI enemies move towards the “target” GameObject, which is assigned in the editor. According to the script reference, this should work:
rigidbody.MovePosition (target.position);
However, it makes my enemy teleport to my player object, not actually travel there like how characterController.Move does.
How can I achieve the same effect as characterController.Move() for a rigidbody?
public float movementSpeed= 5f; //for instance
void Update() {
Vector3 direction = (target.transform.position - transform.position).normalized;
rigidbody.MovePosition(transform.position + direction * movementSpeed * Time.deltaTime);
}
EDIT: fixed a bug.
rigidbody is depreciated use GetComponent()
Vector3 direction = (target.transform.position - transform.position).normalized;
GetComponent().MovePosition(transform.position + direction * movementSpeed * Time.deltaTime);