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
1
Question by mlkielb 1 · Sep 07, 2010 at 01:31 AM · aitrajectorycannonball

Calculate and apply trajectory formula to AI enemy weapon

My enemy ships have cannons that rotate up and down around their x axis, with the barrels pointing down the z axis.

I am trying to use the typical Slerp statement that you would use to smoothly rotate an object from the current rotation to the target rotation. I usually use Slerp to rotate around the Y axis, but am trying to alter it to use the x axis. I'm trying to calculate the target rotation by using a formula to find the required angle to reach the distance of a target:

alt text

Here is my code:

var distance = Vector3.Distance(transform.position, target.position);

var reqAngle = .5*Mathf.Asin((gravityV*distance)/(bulletSpeed*bulletSpeed));

turretOne.transform.rotation = Quaternion.Slerp (turretOne.transform.rotation, Quaternion.LookRotation(targetDirection), TurretRotationSpeed * Time.deltaTime); turretOne.transform.eulerAngles = Vector3(0, turretOne.transform.eulerAngles.y, 0);

cannonOne.transform.rotation = Quaternion.Slerp (cannonOne.transform.rotation, Quaternion.LookRotation(Vector3(reqAngle,targetDirection.y,0)), TurretRotationSpeed * Time.deltaTime); cannonOne.transform.eulerAngles = Vector3(cannonOne.transform.eulerAngles.x, targetDirection.y, 0);

The Cannons are attached to the turret. The turrets can target the player character just fine, I left that code out. But I can get the cannons to adjust to the proper angle to shoot their projectiles the correct distance.

Thanks!

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

2 Replies

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

Answer by mlkielb 1 · Sep 07, 2010 at 02:27 PM

I finally got it. Incase anyone want to do the same thing here is the code I used. I didn't get the Slerp working, but am not to concerned about it right now.

var bulletSpeed = 55.0; var gravityV = -9.5; var reqAngle = .5*Mathf.Asin((gravityV*distance)/(bulletSpeed*bulletSpeed)); reqAngle = .75*(reqAngle*100);

turretOne.transform.rotation = Quaternion.Slerp (turretOne.transform.rotation, Quaternion.LookRotation(targetDirection), TurretRotationSpeed * Time.deltaTime); turretOne.transform.eulerAngles = Vector3(0, turretOne.transform.eulerAngles.y, 0);

cannonOne.transform.eulerAngles = Vector3(reqAngle, turretOne.transform.eulerAngles.y, turretOne.transform.eulerAngles.z);

I had to cut the orriginal result of the angle buy 3/4 because of the enemy model setup. That's why I multiplied by .75. The cannons are attached to the turrets, that's why I included part of the turret code into the code.

Comment
Add comment · Show 2 · 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 Thet Naing Swe · Oct 04, 2010 at 07:58 AM 0
Share

What do u mean the enemy model setup? Is that because you scaled down the enemy model or not?

avatar image mlkielb 1 · Oct 06, 2010 at 07:02 PM 0
Share

I don't think the scale of the object would matter. I was referring to other factors such as the height of the turret compared to the target that weren't factored into the equation. This equation is for two points that are perfectly equal height. If the orrinal trajectory equation works for you, you don't need to mess with the result like I did. $$anonymous$$y enemies were slightly over shooting their targets, so I multiplied the result by .75 to lower the cannons a bit.

avatar image
0

Answer by jlcnz · Mar 18, 2012 at 06:22 AM

The reason this doesnt work, is because the result is in radians rather than degrees.

reqAngle * 180 / Pi

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

1 Person is following this question.

avatar image

Related Questions

AI Turret Aiming 2 Answers

Shoot mortar on clicked floor 1 Answer

Projectile trajectory 1 Answer

NavMeshAgent Calculate Jump Trajectory 1 Answer

Assign gameObject to a variable at runtime 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