- Home /
AI scripting Question.
Hi There! So, I recently started scripting AI and I see to be having a problem... The AI will start following me, but if I jump it will start flying towards me... And wont go back onto the ground... And I can not seem to figure out why? Here's the code
if(CanWalk == true)
{
spider.position += spider.forward *moveSpeed * Time.deltaTime;
spider.rotation = Quaternion.Slerp(spider.rotation, Quaternion.LookRotation(Character.position - spider.position), rotationSpeed*Time.deltaTime);
Debug.Log ("The Spider Is Walking Towards Player");
}
Answer by pako · Apr 02, 2014 at 07:49 AM
When you specify spider.rotation you use as reference the Character.position. However this takes the y component of the characters position too, but you don't want to do that if you want the spider to stay on the ground when you jump. So try this:
float groundLevel; //set this to the y value for your ground level
if(CanWalk == true)
{
Vector3 characterHorizontalPosition = new Vector3(Character.position.x, groundLevel, Character.position.z);
spider.position += spider.forward *moveSpeed * Time.deltaTime;
spider.rotation = Quaternion.Slerp(spider.rotation, Quaternion.LookRotation(characterHorizontalPosition - spider.position), rotationSpeed*Time.deltaTime);
Debug.Log ("The Spider Is Walking Towards Player");
}
Answer by Tol'Satha · Apr 01, 2014 at 06:24 PM
You are rotating the spider to constantly face you and move forward. What happens is that when you jump, the spider looks slightly up into the air (towards you), and the forward vector that you are moving it by is now also facing into the air, so when you move him forward, he leaves the ground. What you should do is add a rigidbody and a collider to make him stick to the ground, and also rotate him to face you only on the Y axis, to prevent him from looking up or down and only follow you on that axis.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141710im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why isn't my enemy pausing in between waypoints? C# 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers
How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#) 3 Answers
.move dose not work with AI C# 1 Answer