• 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 Kergal · Aug 06, 2012 at 07:34 PM · c#velocitycannonball

C# Shooting a cannonball (ballistics)

Hey guys, I am working on a TowerDefense/ MOBA game . Currently I am dealing with an issue concerning my CannonTower. Please take a look at the screenshot I have uploaded (the link). As you can see the tower is supposed to shoot at the little mummy fellow.
I have tried several scripts and ways to make this happen. First I tried all transform.Translate commands I knew, much like the AddForce commands the result was not really what I had in mind. I then stumbled onto a thread in this forum concerning the issue written in (of course -.-) unity script. I tried to translate this script to c# - I had to leave out a part to make it work. The cannonball is moving the way I want it to fly, but as far as I understand the script was never really intended to "hit" the target. I tried to understand all the Mathf. functions - but well you know.. english.. math.. + beginner in coding = bad. It would be great if I could find a way to make the cannonball move exactly the way it is in this script ,BUT increase the entire flight accordingly. If the enemy is very close - the ball would have to fly only a little faster,however if the enemy is further away the ball would have to fly a lot faster.

This is the original unity script code :

function BallisticVel(target: Transform, angle: float): Vector3 {

var dir = target.position - transform.position; // get target direction var h = dir.y; // get height difference

     dir.y = 0;  // retain only the horizontal direction
 
     var dist = dir.magnitude ;  // get horizontal distance
 
     var a = angle * Mathf.Deg2Rad;  // convert angle to radians
 
     dir.y = dist * Mathf.Tan(a);  // set dir to the elevation angle
 
     dist += h / Mathf.Tan(a);  // correct for small height differences
 
     // calculate the velocity magnitude
     var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
 
     return vel * dir.normalized;

}

    var myTarget: Transform;  // drag the target here

var cannonball: GameObject; // drag the cannonball prefab here var shootAngle: float = 30; // elevation angle

function Update(){ if (Input.GetKeyDown("b")){ // press b to shoot var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity); ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle); Destroy(ball, 10); } }

I translated the code but had to leave out the "angle" part - because I couldn't make it work.

This is what I got:

GameObject ball = Instantiate(cannonballprefab,shootCannonPos,Quaternion.identity)as GameObject; ball.rigidbody.velocity = BallisticVel(enemyObject);

public Vector3 BallisticVel(Transform target){ Vector3 dir = target.position - mySelf.transform.position; float h = dir.y; dir.y =0; float dist = dir.magnitude; dir.y = dist; dist += h; float vel = Mathf.Sqrt(dist*Physics.gravity.magnitude); return vel*dir.normalized; }

Here is the LINK to the scene:

[IMG]http://i45.tinypic.com/10zz4nl.png[/IMG]

in case that link won't work :

http://i45.tinypic.com/10zz4nl.png

All this colliding,exploding,health stuff shouldn't be an issue..just that fly with a curve - and actually hit the damn target is ;)

Thanks guys!

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

Answer by aldonaletto · Aug 13, 2012 at 08:24 PM

This script is from an answer of mine, and there's a C# version in http://answers.unity3d.com/questions/235861/use-a-vector3-position-to-calculate-where-a-bomb-f.html

The cannonball must be a simple rigidbody. The BallisticVel function was derived from a basic formula intended to hit a target at the same height. I added some extra calculation to compensate to some extent for height differences - this works fine when the horizontal distance is bigger than the vertical distance (3 times or more).

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

need teleport script fixed slightly 1 Answer

C# Script Template - how to make custom changes? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Health Regen 1 Answer

press e to speek -1 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