Keep AI @ right height, walk down hills and up and stuff.

Hi, I’m writing a simple AI script that follows waypoints and can chase the player.
I’m wondering how to keep the AI at right height / on the ground, cos right now it flies around :stuck_out_tongue: I don’t want it to have any problems going down a hill or up one (unless its too steep of course)…Could anyone tutor me on how to do this? Here is the code at the moment:

using UnityEngine;
using System.Collections;
public class blablabla : MonoBehaviour
{
	static float speed = 60;
	public Transform[] waypoint;
	public AudioClip[] feukaSounds;
	float distanceToPlayer;
	int currentWaypoint;
	public float feukaTimer = 3.0f;
	enum SeekerState
	{
		seeking,
		walkingToNext,
		playerSpotted,
		stuck
	};
	SeekerState seekerState = SeekerState.walkingToNext;
	void Update()
	{
		switch(seekerState)
		{
			case SeekerState.seeking:
			seek();
			break;
			case SeekerState.walkingToNext:
			walkToNext();
			break;
			case SeekerState.playerSpotted:
			chase();
			break;
			case SeekerState.stuck:
			unstuck();
			break;
		}
		transform.rotation = Quaternion.identity;
	}
	void seek()
	{
		
	}
	void walkToNext()
	{
		if(currentWaypoint < waypoint.Length)
		{
			Vector3 target = waypoint[currentWaypoint].position;
			Vector3 moveDirection = target - transform.position;
			Vector3 velocity = rigidbody.velocity;
			if(moveDirection.magnitude < 1)
				currentWaypoint++;
			else
			{
				double direction = Mathf.Atan2(moveDirection.x,moveDirection.z);
				moveDirection.x += Mathf.Sin((float)direction);
				moveDirection.z += Mathf.Cos((float)direction);
				velocity = moveDirection.normalized*speed;
			}
			rigidbody.velocity = velocity;
		}
		else
			currentWaypoint = 0;
		if(Vector3.Distance(GameObject.Find("Player").transform.position, gameObject.transform.position) < 100f)
			seekerState = SeekerState.playerSpotted;
	}
	void chase()
	{
		if(feukaTimer <= 0)
		{
			audio.PlayOneShot(feukaSounds[Random.Range(0,feukaSounds.Length)], 100f);
			feukaTimer = Random.Range(5, 15);
		}
		else
			feukaTimer -= Time.deltaTime;
		if(Vector3.Distance(GameObject.Find("Player").transform.position, gameObject.transform.position) < 300f)
		{
			Vector3 target = GameObject.Find("Player").transform.position;
			Vector3 moveDirection = target - transform.position;
			Vector3 velocity = rigidbody.velocity;
			if(moveDirection.magnitude < 1)
			{
				// Kill player
			}
			else
			{
				double direction = Mathf.Atan2(moveDirection.x,moveDirection.z);
				moveDirection.x += Mathf.Sin((float)direction);
				moveDirection.z += Mathf.Cos((float)direction);
				velocity = moveDirection.normalized*speed;
			}
			rigidbody.velocity = velocity;
			
		}
		
	}
	void unstuck()
	{
		
	}
}

Since you seem to be using rigidbody for movement physics, just make sure you have checked “use gravity” in rigidbody component in inspector. While testing to make sure for 100% you are using gravity you can add such line at beggining of Update in your AI component:

rigidbody.useGravity = true;

If your monster still “flies”, try making them heavier, by increasing rigidbody mass (inspector or in code: rigidbody.mass = 5;)

If enemies still fly in straight line between two points, you could try to use character controller for them instead.

And now most important thing. Make sure u have “isKinematic” set to false (kinematic rigidbodies doesn’t use dynamic physics, like newtons laws).

For beeing sure at 100% that you are not using “isKinematic”, add at begining of Update line:

rigidbody.isKinematic = false;

@Ryba,
The gravity is enabled, kinematic is off and I increased the mass.
My “monster” still flies T_T

Thanks tho :stuck_out_tongue:

Apply this script to all your waypoints…

using UnityEngine;
using System.Collections;

public class waypointPosition : MonoBehaviour {
	RaycastHit hit;
	void Start() {
		if(Physics.Raycast(transform.position, -Vector3.up, out hit)) {
			transform.position = hit.point + new Vector3(0, 0.15f, 0);		
		}
	}
	
}

This will keep all your waypoints to ground…
May be your waypoints are up in the sky… and the target direction is calculated according to that