- Home /
controller.simplemove having issues with vector3.forward
EDIT: See my comment response below this post
I am trying to simply move my enemy forward (using the character controller) by calling SimpleMove.
First off, I got this to work no problem with my player class. I copied the bits and adjusted them and applied them to my enemy class and its not working. The enemy sort of jerks in the forward direction for a few frames, then immediately goes the wrong direction for about 1.5 seconds, then jerks into the forward direction, etc...etc...
I've included this wonderful illustration to show what I mean. He move forward, then slides the wrong way for a bit, then forward. I have the character controller and character motor set up identical to my player class and its just not working. Any ideas?
using UnityEngine; using System.Collections;
public class EnemyController : MonoBehaviour {
public int health;
public AudioClip hit;
public AudioClip die;
float distanceCheck;
Vector3 desiredDirection;
public Transform PlayerPosition;
public float runSpeed;
public float rotationSpeed;
CharacterController controller;
// Use this for initialization
void Start () {
animation["gethit"].layer = 2;
animation["gethit"].speed = 1f;
animation["waitingforbattle"].layer = 1;
animation["die"].wrapMode = WrapMode.ClampForever;
animation["die"].weight = 1;
animation["die"].layer = 3;
}
// Update is called once per frame
void Update () {
controller = GetComponent<CharacterController>();
distanceCheck = Vector3.Distance(PlayerPosition.position, transform.position);
desiredDirection = (PlayerPosition.position - transform.position);
controller.SimpleMove(transform.forward * runSpeed);
if (health <=0)
{
animation.Play("die");
animation.wrapMode = WrapMode.ClampForever;
}
else
animation.CrossFade("waitingforbattle");
}
public void getHit(int damage)
{
health -= damage;
if (health > 0)
audio.PlayOneShot(hit);
else
audio.PlayOneShot(die);
animation.Play("gethit");
}
}
Alright...so I found out that for whatever crazy reason the following line of code causes this to happen.
else
animation.CrossFade("waitingforbattle");
Why does playing an animation cause my player to run erratically? If I comment this out, he move properly.
Answer by aldonaletto · Jul 01, 2012 at 06:50 AM
I suspect you may have some object with a collider childed to your character - the character controller behaves weirdly when some child collider touches its capsule: it "thinks" to be colliding with its own child and moves to weird directions. If you have such object, set its collider Is Trigger field, and the character will ignore it.
You were right. I have a capsule collider on the enemy's weapon. I use it to detect if he hits the player. Setting it to trigger should allow me to still detect hits. Thanks for the solution.
Answer by ternitet · Feb 06, 2015 at 10:04 AM
Bit late, but if others come this way with the same question, I'd like to provide my insights on this very problem:
Applying a SimpleMove() to a GameObject with a CharacterController may fail if that GameObject has a NavMeshAgent component attached.
Your answer
Follow this Question
Related Questions
how to ignore transform.position.y 3 Answers
transform shooting forward 2 Answers
Difference between transform.forward and transform.position.z? 1 Answer
How do I add a forward and backward move by a swipe to my iOS script? 1 Answer
movement doesnt stop when jump pad is tapped at the same time 0 Answers