- Home /
Check if my AI is grounded
I've created my own AI, very simple though but I'm in need of help at the moment.
The problem is that my AI freezes in the air and I know where the problem is in the script but I have not idea how to solve it.
{
public float Stop;
public int MoveSpeed;
public Transform Target;
public Transform mytransform;
private float groundRange = 1.2f;
public GameObject Character;
public float distancebetweenobjects;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Ray vision = new Ray (transform.position, Vector3.forward);
RaycastHit x;
float dist = Vector3.Distance(mytransform.position, Target.position);//distance between AI and player
RaycastHit y;
Ray isgrounded = new Ray (transform.position, Vector3.down);
if (Physics.Raycast (isgrounded, out y, groundRange)) //Problem is here, the groundrange
{
if(dist <= distancebetweenobjects && dist >= Stop)
{
mytransform.LookAt(Target);
mytransform.position += mytransform.forward * MoveSpeed * Time.deltaTime;
Character.animation.CrossFade("Armature|SoldierRun");
}
else
{
Character.animation.CrossFade("Armature|SoldierIdle");
}
if(dist <= Stop)
{
mytransform.LookAt(Target);
Character.animation.Play("Armature|SoldierHit");
}
}
}
}
Right now if the AI gets above 1.2f or whatever I set the groundrange to, he freezes in the air. I saw other questions about giving the AI a rigidbody but that didn't work very well as he just flew past the screen when he started running.
Any ideas?
Answer by skylem · Jan 14, 2015 at 09:49 PM
theres a couple ways you can do this and it partly depends on how u've setup your character if you were using a CharacterController to move your player this actually stores a value for .isGrounded so u can access ur charactercontroller and check that value, But given that you are raycasting im going to assume you don't have one on your character, below i'll paste a basic raycast designed for ground detection
//this is our direction/vector that we raycast
Vector3 downCheck = transform.TransformDirection(Vector3.down * 0.1f);
// this is just a copy of our raycast so we can see whats happening in the Scene.
Debug.DrawRay(transform.position, downCheck, Color.green, 1f, true);
// this is our actual raycast, it will use the downCheck to see if there is anything //directly beneath our character
// if there is anything there it will set a boolean to true
// otherwise it sets it to false
if(Physics.Raycast (transform.position, downCheck, 1f)) {
somethingdown = true;
} else { somethingdown = false; }
edit -- Falling.
// then lastly, we check the boolean(True/False) before we apply some falling this setup basically says if nothing is directly below us we will fall.
if(somethingdown == false) {
transform.position = new Vector3(transform.position.x, Physics.gravity.y * Time.deltaTime, transform.position.z);
}
EDIT -- Revision of Code.
Vector3 downCheck = transform.TransformDirection(Vector3.down * 0.1f);
Debug.DrawRay(transform.position, downCheck, Color.green, 1.5f, true);
/// Note this first Debug is just to see your raycast you can delete it after your sure its working right.
if(Physics.Raycast (transform.position, downCheck, 1.5f))
{
somethingdown = true;
}
else
{
somethingdown = false;
}
if (somethingdown == true)
{
if(dist <= distancebetweenobjects && dist >= Stop)
{
mytransform.LookAt(Target);
mytransform.position += mytransform.forward * MoveSpeed * Time.deltaTime;
Character.animation.CrossFade("Armature|SoldierRun");
}
else
{
Character.animation.CrossFade("Armature|SoldierIdle");
}
if(dist <= Stop)
{
mytransform.LookAt(Target);
Character.animation.Play("Armature|SoldierHit");
}
}
if(somethingdown == false)
{
transform.position += Physics.gravity / 10;
}
}
}
I'm sorry, I dont quite follow you here what this code does
ill make some more notes above -- Notes added if i missed anything or was unclear on any point let me know and i'll see if i can explain it more clearly.
I'm sorry but I cant get it to work
The AI acts the same when put in the code you wrote into my script except the falling part as he just flys(Dont know the word) up in the air and stops there.
Vector3 downCheck = transform.TransformDirection(Vector3.down * 0.1f);
Debug.DrawRay(transform.position, downCheck, Color.green, 1.5f, true);
/// Note this first Debug is just to see your raycast you can delete it after your sure its working right.
if(Physics.Raycast (transform.position, downCheck, 1.5f))
{
somethingdown = true;
}
else
{
somethingdown = false;
}
if (somethingdown == true)
{
if(dist <= distancebetweenobjects && dist >= Stop)
{
mytransform.LookAt(Target);
mytransform.position += mytransform.forward * $$anonymous$$oveSpeed * Time.deltaTime;
Character.animation.CrossFade("Armature|SoldierRun");
}
else
{
Character.animation.CrossFade("Armature|SoldierIdle");
}
if(dist <= Stop)
{
mytransform.LookAt(Target);
Character.animation.Play("Armature|SoldierHit");
}
}
if(somethingdown == false)
{
transform.position = new Vector3(transform.position.x, Physics.gravity.y * Time.deltaTime, transform.position.z);
}
Would it be easier if I used a charactercontroller ins$$anonymous$$d of raycasting?
Your answer
Follow this Question
Related Questions
Refer to a parent Rigidbody 1 Answer
How to use AddForce on FPC 0 Answers
Change player movement 0 Answers
Add force which throws back an object 3 Answers
Limit Rotation of Rigidbody to 45 Degrees (Handlebars on a Bicycle) 1 Answer