- Home /
Enemies Moving Through Terrain Help
I have a problem with my enemies that move toward the player actually dropping down halfway though my terrain once they begin to get close. Essentailly they are moving to the direct center of the player, which puts them half way below my terrain. The enemies have Rigidbodies, with is kinematic checked. Here is the script that moves them.
using UnityEngine;
using System.Collections;
public class redCubeAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
float distance;
private Transform myTransform;
void Awake(){
//caches transform
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
//set target to player
target = go.transform;
}
// Update is called once per frame
void Update () {
//check distance to target
distance = Vector3.Distance(gameObject.transform.position, target.transform.position);
//draw line to targer
Debug.DrawLine(target.position, myTransform.position);
//look at player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(distance < 800){
//move to target
myTransform.position +=myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
What do I need to change to make the enemy models not clip through my terrain? Thanks for any help!
Answer by T27M · Sep 22, 2012 at 11:40 PM
You could cast a ray from the enemy to the terrain and use this to update this as the enemies y position.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
I need help with my enemy collision. 1 Answer
Enemy AI With changing Player 0 Answers
making own AI Basic 2 Answers
How to make basic AI in a 2d game? 4 Answers