Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 RealSoftGames · Jul 19, 2015 at 02:10 PM · raycastailookat

issue with AI shooting other AI

Link to Gap between the ray shots and the target when raycast variance is set to 0

https://www.dropbox.com/s/hogiq4wyojc7kdi/Gap.png?dl=0

Hey guys as the title states, i'm having an issue with my AI soldier shooting at my Zombie AI,

im using transform.LookAt(target) on my Soldier to look and shoot a raycast at my Zombie with a (-0.02,0.02) offset for that realistic aiming

these 2 functions pretty much make up the issue, the lookAt is looking somewhere off center of all my Zombie Ai, and when i use a LookAt their actual transform it is at the ground position, so to counter this i made an Aim GameObject and attached it to each of the AI and adjusted it accordingly, so when my Soldiers cycle through the zombies in the area they target the closest one and then set their lookat position to their child Aim location, but this did not seem to work at all, i'm still getting the error. is their a way i can fix this transform.LookAt to look at my target accordingly. i have also adjusted where the raycasts get shot from, i usually use a custom empty GameObject ShootFromPosition and put that at the Soldiers Weapon Barel position along with the weapon effects.

the image provided shows that it will hit sometimes, but at majority of the time it hits way off its target always to the right.

 void LookAtTarget ()
     {    
         //stop moving when looking at target!
         agent.SetDestination(transform.position);

             //this used to be that actual Targets position, but created a Aim location attatched to the targets to try fix the issue, but no luck with doing so.
         transform.LookAt(aim.position);
     }
     
     void Attack ()
     {        
         if (Time.time > attackSpeed) {            
             
             Effects ();            
             
             RaycastHit hit;
             Vector3 direction = transform.TransformDirection (Vector3.forward);
             
             //Weapon Spread
             direction.x += Random.Range (-BulletSpreadFactor, BulletSpreadFactor);
             direction.y += Random.Range (-BulletSpreadFactor, BulletSpreadFactor);
             direction.z += Random.Range (-BulletSpreadFactor, BulletSpreadFactor);
             
             if (Physics.Raycast (shootFrom.position, direction, out hit))
                 Debug.DrawRay (shootFrom.position, direction * 100, Color.blue, 5f);
             
             
             if (hit.collider.tag == ("Enemy")) { 
                 hit.collider.GetComponent<Health> ().curHp -= Damage;
                 //move to effects later
                 Instantiate (bloodSplat, hit.point, Quaternion.identity);                
             }            
             else if (hit.collider.name == "Head") {
                     hit.collider.transform.root.GetComponent<Health> ().curHp -= hit.collider.transform.root.GetComponent<Health> ().maxHp;
                     Instantiate (bloodSplat, hit.point, Quaternion.identity);
                     hit.collider.GetComponent<SphereCollider> ().enabled = false;
                 }
             //random range this for some nice fire variences
             attackSpeed = Time.time + Random.Range (nextAttack / 2, nextAttack * 2);
         }        
     }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Simulating Mouse Input for AI 0 Answers

Visible GameObjects for AI 0 Answers

Follow up to AI Pathfinding Question 0 Answers

AI Look at X axis more efficient code 1 Answer

Need Help making Pong AI beatable 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