- Home /
Check for empty space relative to an object
I have an enemy AI. Right now, the AI simply makes the enemy move towards the player. The problem with it is that the center of the player is taller than the enemy, causing the enemy to float in mid-air. Here is my enemy AI. Yes, this is Boo:
import UnityEngine
class EnemyAI (MonoBehaviour):
public target as Transform
private myTransform as Transform
public rotateSpeed as int
public moveSpeed as int
public maxDistance as single
private defaultYPos as single
def Awake():
myTransform = transform
def Start ():
go as GameObject = GameObject.FindGameObjectWithTag('Player')
target = go.transform
defaultYPos = transform.position.y
rotateSpeed = 5
moveSpeed = 3
maxDistance = 2.3
def Update ():
Debug.DrawLine (target.position, myTransform.position, Color.yellow);
//Look at the target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), (rotateSpeed * Time.deltaTime))
//If it isn't too close
if Vector3.Distance(target.position, myTransform.position) > maxDistance:
//Move towards the target
myTransform.position += ((myTransform.forward * moveSpeed) * Time.deltaTime)
if transform.position.y != defaultYPos:
transform.position.y = defaultYPos
As a temporary fix, I locked the y position. Since I can't do that for ever, my thought is to check "If the unit under myself is free, move down. Else, don't."
So, how would you check for empty space relative to itself?
Since I highly doubt anyone knows Boo, you can answer in C#. I know basic C# syntax, so I can convert it over. Thanks in advance!
Answer by getyour411 · Apr 25, 2014 at 10:51 PM
I do it like this (basically a Y work around too) and it's working fine for me so far
Vector3 lookDir = (myTarget.position - myTransform.position);
lookDir.y = 0f;
myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (lookDir), rotationSpeed * Time.deltaTime);
I don't do that in Update() though since I don't need it calculated every frame (except when the player is in range)
I see where you're going with that, and it worked. But it still raises one problem: What if the y value needs to change, as in if the player goes up a slant? $$anonymous$$aybe you could change lookDir.y/transform.position to something based on the player?
I've been play testing my game for some time and it's never been a problem visually for me and my test terrain is covered with hills and valleys. If you wanted to get fancy, assu$$anonymous$$g the enemy has a neck bone you could grab a reference to that in your script and rotate that.
I'm talking about the transform.position. Say the player moves up a hill, the enemy wants to move up as well. $$anonymous$$y idea is that every frame, it checks if there is open space below it, similar to an isGrounded check. If there is open space, it would move down. But how would you check for open space below itself?
Just noticed you are using transform.position to move. I'll wreck your day by telling you don't do that I suppose, but you'll want to invest the time in that otherwise you'll have all kinds of issues.
Do some searching on Enemy movement with rigidbody or charactercontroller.
Aha, I just tried a rigidbody and it worked. Ill look into the alternatives too. It works, but it is pretty glitchy. Thanks!