0
votes

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.

2
Make sure the collider is actually active by ticking the checkbox next to the component title in the inspector. - Baher Ramzy
Please do not use the "code snippet" editor, it is only for Javascript/HTML code. Use the { } button instead. - LearnCocos2D

2 Answers

0
votes

Try adding 1000 or 10000 force ... from the looks of it you're using fractions of 1... if you have a mass of 1 it still takes over 1 force to make it move. If you add 1000 force and his mass is 1 and he still doesn't move, then make sure he's not set as kinematic in his rigidbody.

If you still aren't moving, something much more insidious is going on. I'd suggest checking you're using the right axis, the right direction.. etc.. things that just couldn't possibly be wrong.

Good luck, hope that helped.

0
votes

Whenever you're using AddForce, try to multiply the directional Vector with your desired speed and the object's Rigidbody mass. Also, uncheck the "isKinematic" checkbox because that kind of "deactivates" the physics engine's calculations on that object. If you want to be accurate you can also manually set the velocity of the object's Rigidbody too.