Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NoGo · Jul 04, 2011 at 07:32 PM · collisionaicollision detectionshootingoncollisionenter

Why does my AI miss me every few feet?

If I am standing right in front of him he will hit me then if I back up a little he will start to miss me. After a few more feet back he will start hitting me again. I think I know the problem but not sure how to fix it. this is the code I have and all the info I think you will need to help me.

On my Bullit prefab I have the following set of var's

Mass: 0.1

Drag: 0

Angular Drag: 0.05

Use Gravity: True

Is Kinematic: False

Interpol: None

Collision Detection : Discrete

Constraints: None

This is the code on my AI

 var LookAtTarget : Transform;
 
 var damp = 6.0;
 
 var bullitPrefab : Transform;
 
 var savedTime = 0;
 
 function Update () 
 {
     
     if(LookAtTarget)
     {          //           Finds the dif between the two         Player position                      Turret Pos
         var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
         
         
         //   rotation of the turret                                  turret rotation,      var,   time that passes between frames
         transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime                                                            *
                                                             damp);
         
         var seconds : int = Time.time;
         var oddeven = (seconds % 2);
         
         if(oddeven)
         {
             Shoot(seconds);
         }
     }
     
     
 }
 
 function Shoot(seconds)
 {
     
     if(seconds != savedTime)
     {
         var bullit = Instantiate(bulltPrefab, transform.Find("SpawnPoint").transform.position, 
                                     Quaternion.identity);
                                     
         bullit.rigidbody.AddForce(transform.forward *1000);
     
         savedTime = seconds;
     }
 }

And This is my code on my buillit prefab

 function OnCollisionEnter(hit : Collision)
 
 {
 
     if(hit.gameObject.tag == "Player")
     {
         HC.LIVES -=1;
         
         Destroy(gameObject);
     }
 
     if(hit.gameObject.tag == "turret")
     {
         
         Destroy(gameObject);
     }
 
 }
Comment
Add comment · Show 6
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 Chris D · Jul 04, 2011 at 07:52 PM 1
Share
  1. Please try to tidy your code a bit

  2. If you think you know what the problem might be, can you share that information? It'll be faster than trying to guess ; )

avatar image Owen-Reynolds · Jul 04, 2011 at 08:07 PM 0
Share

Get a lot more data on where it misses, hits and how. Does NESW to the target matter? Does it always miss by shooting too far down? Is it just the one range band, or are there more? The bullet shoots at the firer's facing -- in the Inspector, is Slerp giving odd rotations?

avatar image NoGo · Jul 04, 2011 at 10:04 PM 0
Share

Sorry for the crappy code I am new to scripting/code. When I pause the game and go frame by frame I see the space it skips each time. Sometimes I am colliding with it and others I am not. It moves by 2.96,0.26,2.6 so if I am not in a space that is in increments of 2.9,.26,2.6 from the AI then it will not detect a hit on me. I need it to move at the same speed but update more often I guess??

I do not fully understand Slerp yet.

avatar image Waz · Jul 04, 2011 at 10:09 PM 0
Share

ChrisD is asking you to at least format the code properly in the question. If it's hard to read, most people will not bother, so you won't get an answer from them.

avatar image NoGo · Jul 04, 2011 at 10:35 PM 0
Share

Is this what you mean? Sorry for being such a newb.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Waz · Jul 04, 2011 at 10:53 PM

The problem is you are trying to use a high-speed bullet. With discreet physics, the bullet jumps forwards each frame - sometimes right over you. Either change the Collision Detection mode to "Continuous Dynamic" (which is expensive), or use raycasting to "fire" bullets rather than trying to model them physically.

Comment
Add comment · Show 1 · 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 NoGo · Jul 05, 2011 at 05:09 AM 0
Share

Ya ray casting another mystery unto its self. That is the next thing I dig into. Thx for all your help and sorry for the shitty code :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnCollisionEnter not working with Platform and Ball game 1 Answer

How to detect correct collision from multiple game objects? Separate collisions 0 Answers

OnCollisionEnter being called without the colliders actually colliding 1 Answer

Help with collisions and destroy please 1 Answer

Smooth movement for the block upon player collision,Smooth box movement 0 Answers


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