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 /
This question was closed Apr 09, 2013 at 07:09 AM by Fattie for the following reason:

there are no write-code-for-me questions on this site, substantial help already given

avatar image
0
Question by dontpanic4242 · Apr 09, 2013 at 12:21 AM · raycastaicollision detection

AI bug issue

Below is a semi intelligent ai script. It works, but it has an issue with the rotation. Locking the rigidbody's rotation x and z. (None of the units are meant to move up and down, as the entire game is set on a flat surface.) Anyway, the ai basically does a barrel roll and prevents the computer to from actually moving around an object.

In addition, locking the y position on the rigidbody for some reason also causes the ai to only look at the target, instead of following the target. The only other codes attached are a instantiating code to fire a projectile, and a code to animate the simple gun to fire.

The code is below, and any thoughts are appreciated.

     #pragma strict
     @script RequireComponent (Rigidbody)
     
     private var myTransform : Transform; 
     private var myRigidbody : Rigidbody;
     
     var target : Transform; // the player
     var movespeed : float = 6.0;
     var turnspeed : float = 1.0;
     var rayDistance : float = 10;
     var mp : float = .35;
     
     
     private var desiredVelocity : Vector3;
     var isGrounded : boolean = false;
     
     function Start () 
     {
         myTransform = transform;
         myRigidbody = rigidbody;
         
         myRigidbody.freezeRotation = true;
     }
     
     function Update () 
     {
         Moving();
         
     }
     
     function Moving()
     {
         //rotation
         var lookDirection : Vector3 = (target.position - myTransform.position).normalized;
         
         var hit : RaycastHit;
         
         var shoulderMultiplier : float = mp;
         
         var leftRayPos : Vector3 = myTransform.position - (myTransform.right * shoulderMultiplier);
         var rightRayPos : Vector3 = myTransform.position + (myTransform.right * shoulderMultiplier);
     
     
         if (Physics.Raycast (leftRayPos, myTransform.forward, hit, rayDistance))
         {
                 Debug.DrawLine (leftRayPos, hit.point, Color.red);
                 lookDirection += hit.normal * 1.0;
         }
         else if (Physics.Raycast (rightRayPos, myTransform.forward, hit, rayDistance))
         {
                 Debug.DrawLine(rightRayPos, hit.point, Color.red);
                 lookDirection += hit.normal * 1.0;
         }
         else
         {
             Debug.DrawRay(leftRayPos, myTransform.forward * rayDistance, Color.yellow);
             Debug.DrawRay(rightRayPos, myTransform.forward * rayDistance, Color.yellow);
     
         }
         
         var lookRot : Quaternion = Quaternion.LookRotation( lookDirection );
     
                 
         myTransform.rotation = Quaternion.Slerp (myTransform.rotation, lookRot, turnspeed * Time.deltaTime);
     
         //movement
         desiredVelocity = myTransform.forward * movespeed;
         desiredVelocity.y = myRigidbody.velocity.y;
     }
     
     function FixedUpdate()
     {
         if(isGrounded)
         {
         myRigidbody.velocity = desiredVelocity;
         }
     }
     
     
     function OnCollisionEnter (collision : Collision)
         {
             if ( collision.collider.gameObject.name == "Floor")
                 {
                     isGrounded = true;
                 }
         }
     function OnCollisionStay (collision : Collision)
         {
             if ( collision.collider.gameObject.name == "Floor")
                 {
                     isGrounded = true;
                 }
         }
     function OnCollisionExit (collision : Collision)
         {
             if ( collision.collider.gameObject.name == "Floor")
                 {
                     isGrounded = false;
                 }
         }
Comment
Add comment · Show 3
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 AlucardJay · Apr 09, 2013 at 02:32 AM 0
Share

It seems to work fine in a 3-dimensional environment : http://www.youtube.com/watch?v=02bXl1mBQXs&t=15m00s

I believe it was suitable for the purpose and audience it was written for, and with the aim of giving the viewer the tools to write their own AI code, to suit their specific purpose.

$$anonymous$$y thoughts are : this is a write-my-code question. I have already done that for you. The rest is up to you ....

avatar image dontpanic4242 · Apr 09, 2013 at 03:07 AM 0
Share

The game is in a 3d world. The ai just isn't meant jump or go up in the y axis.

So what I'm asking is if I'm missing something in the code that's telling the bot that they can rotate in the x and z. If it isn't I'll comb over the rest of the asset in an attempt to find the problem. I'm not asking anyone to write my code.

avatar image AlucardJay · Apr 09, 2013 at 03:18 AM 0
Share

If you click on the link provided in my first comment, you'll see the NPC moving in a 3-Dimensional environment. I can tell just by looking at the conditionals in your collision events that you havn't done all of my NPC videos, therefore you are asking questions about things you have yet to complete.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Obstacle avoidance fine tuning assistance. 0 Answers

Implementing BUG2 algorithm 0 Answers

AI Field of vision 1 Answer

Using box collider to determine if AI should stop help 1 Answer

How can I draw a ray for direction object is moving? 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