Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 smugleafdev · Jan 11, 2021 at 04:41 AM · projectileaimaiming

Enemy AI prediction aiming isn't working as expected

I have used three, different, clever algorithms to use the power of math to predict exactly where to aim to hit a moving target. The results are buggy and jittery, both jumping around randomly and not particularly accurate otherwise.

I'm going to try to list everything that may be relevant, because I cannot seem to figure this out on my own.

These algorithms all require getting a few variables. Target position and velocity, gun position and velocity, projectile scalar.

  • Scalar (projectile speed float) is easy, so I probably didn't mess this one up. It's just the float speed divided by the fixed update time. So my 500f bullet speed / 0.02 fixed delta time = 10f. Bullet firing code looks like instBulletRigidBody.AddForce(forward * 500f);

  • Target position I must have right, because if I stand still, they hit the target player dead center.

  • Target velocity: no ridigbody to steal velocity from, so I just calculate it by recording the last position (previous fixed update's current position) and subtracting that from the current position, divided by delta time like so: targVel = (targetCurrentPos - targetLastPos) / Time.fixedDeltaTime;

  • Gun position: I grab the transform for the emitter on Start, Gizmos.DrawSphere(myPos, 0.05f); shows it exactly where it should be.

  • Gun velocity: Calculated just like target velocity: myVel = (myPos - myLastPos) / Time.fixedDeltaTime; Though, the enemy doesn't even move except to rotate, so whatever.

    Finally, I use the resulting V3 from the above algorithms (the direction to aim) and rotate the enemy/gun with transform.LookAt(aimAt); before firing (or every fixedupdate), and the result is the atrocious gif above. If the player doesn't move at all, the accuracy never even peaks 50%. Something is horribly wrong.

    Doubt it's relevant, but this is how simply my player is moving in Update:

    Vector3 move = new Vector3(2f * Time.deltaTime, 0, 0);

    transform.Translate(move);

    As a note I tried the "dumb" way that doesn't take into account how long a projectile will take to reach the target's new position, and it's actually more accurate than my attempts at doing it the right way. At least, up until the target is moving away from the enemy.

    If you have any ideas what I should look into, please let me know. I've wasted over ten hours on this.

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

1 Reply

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

Answer by smugleafdev · Jan 11, 2021 at 02:14 PM

Dear fucking god I'm stupid.

Doubt it's relevant, but this is how simply my player is moving in Update:

moving in Update

Update

I am quite upset with myself. I have literally lost 15+ hours trying everything and it's because I put the player movement in Update instead of FixedUpdate. I'm going to go cry now.

[SOLVED]

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

111 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 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 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 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 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

Align rotation to a vector3 point 2 Answers

How do you do this :( 1 Answer

Character wont stay aiming 0 Answers

weapon aim when shooting 0 Answers

Aiming in the direction of cursor in third person. 2 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