- Home /
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 :P 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()
{
}
}
Answer by ryba · Jan 17, 2013 at 09:01 PM
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;
Answer by Imankit · Jan 18, 2013 at 08:10 AM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613091118im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AI Enemies? 1 Answer
how to lock screen? 1 Answer
How to kill enemies by jumping on their heads? 1 Answer
Can't Understand GetComponent C# 2 Answers
How to make a third person free raom? 2 Answers