I have been trying to find a solution to my problem involving a game object in my scene that walks through elevated terrain and rocks. The rocks have colliders and the enemy object has a collider and a rigid body. I have attached my script which moves the enemy around when the FPS gets withing a certain range. This all works fine but its walking through rocks and elevated terrain. I have tried many suggestions so far but nothing seems to work.
using UnityEngine;
using System.Collections;
public class CreatureWalk : MonoBehaviour {
public int totalSecs = 0;
public GameObject explosion;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
GameObject GM = GameObject.Find ("GameManager");
//Get Game Manager Component on Object
GameManager GM_Component = GM.GetComponent<GameManager> ();
if (GM_Component.GameOver) {
animation.Play ("idle");
} else if (GM_Component.FlowerHits > 30) {
while (totalSecs < 5000) {
animation.Play ("hit");
totalSecs += 1;
}
//Destroy (gameObject);
//create explosion
Instantiate (explosion, transform.position, transform.rotation);
Invoke("Disable",3);
Invoke("Reenable",3);
Vector3 currentPosition = this.transform.position; //get current position of creature
transform.position = new Vector3(currentPosition.x+ Random.Range(-20.0F, 20.0F),0.2f,currentPosition.z +Random.Range(-20.0F, 20.0F));
totalSecs =0;
GM_Component.FlowerHits =0;
} else {
animation.Play ("walk");
MoveTowardsTarget ();
}
}
private void Disable() {
gameObject.SetActive(false);
//Debug.Log ("Disable");
}
private void Reenable() {
gameObject.SetActive(true);
//Debug.Log ("Re-enable");
}
//move towards a target at a set speed.
private void MoveTowardsTarget() {
//the speed, in units per second, we want to move towards the target
float speed = 1;
//move towards the fps
Vector3 targetPosition = new Vector3(Camera.main.transform.position.x,0,Camera.main.transform.position.z);
Vector3 currentPosition = this.transform.position;
transform.LookAt (targetPosition);
//first, check to see if we're close enough to the target
if (Vector3.Distance (currentPosition, targetPosition) < 30f) {
Vector3 directionOfTravel = targetPosition - currentPosition;
directionOfTravel.Normalize ();
this.transform.Translate (
(directionOfTravel.x * speed * Time.deltaTime),
(directionOfTravel.y * speed * Time.deltaTime),
(directionOfTravel.z * speed * Time.deltaTime),
Space.World);
//this.rigidbody.MovePosition(directionOfTravel);
// (directionOfTravel.x * speed * Time.deltaTime),
// (directionOfTravel.y * speed * Time.deltaTime),
// (directionOfTravel.z * speed * Time.deltaTime));
//this.rigidbody.AddForce(
// (directionOfTravel.x * speed * Time.deltaTime),
// (directionOfTravel.y * speed * Time.deltaTime),
// (directionOfTravel.z * speed * Time.deltaTime),ForceMode.VelocityChange);
//rigidbody.AddForce(transform.forward * 10);
} else
animation.Play ("idle");
}
}
I have tried to move with AddForce as suggested as you can see by the commented code but the enemy just walks in place.
The creature has a rigidbody, gravity and kinematics checked.