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
0
Question by oskanaan · Jul 05, 2014 at 03:03 AM · javascript3dprojectile

Projectile movement in 3D

I am trying to simulate a projectile artillery shot, I am using the projectile formulas to simulate this movement, but the problem is, I am not finding a way to transform this into 3D coordinates, I can get only the x and y.

What I want to do is transform the (x,y) coordinates I am calculating alongside the initial forward Vector of the gameobject associated with the following script:

 #pragma strict
 
 public class ArtilleryShot extends MonoBehaviour{
     
     var startTime:float ;
     var target:Transform;
     private var destination:Vector3;
     private var distanceToDestination:float;
     private var velocity:float = 20.0;
     private var vx:float;
     private var vy:float;
     private var g:float = 10.0;
     private var x0:float;
     private var y0:float;
     var launchExplosion:GameObject;
     var explosion:GameObject;
     var damage:float;
     var launchPoint:GameObject;
     
     function Start(){
         var explosion:GameObject = GameObject.Instantiate(launchExplosion,transform.position,transform.rotation);
         explosion.transform.localScale = Vector3(4,4,4);
         
         startTime = Time.time;
         destination = target.position;
         distanceToDestination = Vector3.Distance(transform.position, destination);
         //velocity = Mathf.Sqrt((distanceToDestination*g)/(Mathf.Sin(Mathf.Deg2Rad*120)));
         velocity = Mathf.Sqrt((distanceToDestination*distanceToDestination*g)/(2*Mathf.Cos(Mathf.Deg2Rad*60)*Mathf.Cos(Mathf.Deg2Rad*60)*(transform.position.y+(distanceToDestination*Mathf.Tan(Mathf.Deg2Rad*60)))));
         vx = velocity * Mathf.Cos(Mathf.Deg2Rad*60);
         vy = velocity * Mathf.Sin(Mathf.Deg2Rad*60);        
         x0 = transform.position.x;
         y0 = transform.position.y;
     }
     
     function Update(){
         var t:float = Time.time-startTime;
         
         if(t==0)
           return;
         var newPos = transform.position;
         
         newPos.x = x0+(vx*t);
         var downY:float = (-.5*g*t*t);
         var y:float = vy*t+downY;
         newPos.y = y0+y;
         
         var distance = Vector3.Distance(transform.position,newPos);
         var angle = Vector3.Angle(launchPoint.transform.forward,newPos);
         //newPos should be projected alongside the forward axis before setting the new position
         transform.position = newPos;
         
     }    
     
     function SetTarget(targetParam:Transform){
         target = targetParam.transform;
     }
     
     function OnTriggerEnter(other:Collider){
         if(other.gameObject.tag=="EnemyGroundUnit" || other.gameObject.tag=="Ground"){
             var explosionGameObject = GameObject.Instantiate(explosion,transform.position,transform.rotation);
             explosionGameObject.transform.localScale = Vector3(4,4,4);
             other.gameObject.SendMessage("TakeDamage",damage);
             Destroy(gameObject);
         }
     }
     
     function SetPointOfOrigin(launchPointParam:GameObject){
         launchPoint = launchPointParam;
     }
 }
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 NorthernVisionStudio · Jul 05, 2014 at 05:29 AM

You are very close! So, your Y component is the up/down and your x is the distance from start. You have the launch vector, which includes height. So, here's my opinion:

  1. start with the launch vector: Vector3 launchVec = launchPoint.transform.forward

  2. get rid of the y value: launchVec.y = 0

  3. Now you have a flat but non-normalized vector. So, then normalize it. That leaves you with a top-view flat direction vector.

  4. Then take that normalized vector and multiply it by your distance (x) value of your formula. Remember, this is a relative vector, so you will be adding it to the position

  5. Finally, add the y value to this result... resultVec.y += y

  6. And, add to existing position

I will leave it up to you to interpret and understand rather than me writing code.

In summary, the strategy is to use a flattened launch vector for the X and just add the Y component for height.

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 oskanaan · Jul 05, 2014 at 11:35 AM 0
Share

Thanks, it works perfectly now!!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Shooting projectile : GameObject or Particles ? 2 Answers

Calculate Projectille impact point 2 Answers

Shooting projectile : GameObjects or Particles? 1 Answer

Inspector Problems or possible miss script. 0 Answers

Throwing a Spear 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