Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SirMcsquizy · Apr 01, 2014 at 04:57 PM · c#aiflying

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");
         }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fr0stbite · Apr 01, 2014 at 06:13 PM 0
Share

Add Rigibody component to the AI

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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");
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges