• 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 george_vasilchenko · Dec 18, 2014 at 09:18 PM · physicsraycastparabola

Projectile trajectory calculation for 3 dimensions

Hey, I have been able to calculate my trajectory parabola for just axis y and z. I've been lost recently by trying to comprehend what should it look like in the 3rd dimension. Can't figure it out. I have used the known formula from Newton in order to find height of a point according to its distance. But this formula does't cover the 3rd dimension, unfortunately. Basically I need my trajectory to be drawn before and till the hit point of my raycast starting from my cam position. Here is my code, help is appreciated.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Trajectory : MonoBehaviour {

 public GameObject point;
 public float angle = 30.0f;
 public int pointsNum = 30;
 public float Vinit = 20.0f;
 public List<GameObject> Points = new List<GameObject>();

 Vector3 targetPoint = new Vector3(0, 0, 0);
 float distance = 0;

 void Start()
 {
     CreatePoints(pointsNum);
 }
 void Update()
 {
     PositionPoints();
     Debug.Log(CheckAngle() + " ; " + CheckDistance());
 }
 void CreatePoints(int num)
 {
     for (int i = 0; i < num; i++)
     {
         GameObject pointPrefab = Instantiate(point, transform.position, Quaternion.identity) as GameObject;
         pointPrefab.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
         Points.Add(pointPrefab);
     }
 }
 void PositionPoints()
 {
     for (int i = 0; i < Points.Count; i++)
     {
         Points[i].transform.position = new Vector3(0, 
                                                    1 + CalcHeight(CheckAngle(), (i + (CheckDistance() / Points.Count)), Vinit), 
                                                    i + (CheckDistance() / Points.Count));
     }
 }
 float CalcHeight(float angle,
                  float distance,
                  float Vinit)
 {
     float Vvert = Vinit * (Mathf.Sin(angle * Mathf.Deg2Rad));
     float Vhor = Vinit * (Mathf.Cos(angle * Mathf.Deg2Rad));
     float x = distance;
     float t = distance / Vhor;
     float g = Physics.gravity.y;
     float u = Vvert;
     float s = (( u * x ) / Vhor ) + 0.5f * g * Mathf.Pow( t, 2 );    
     return s;
 }
 float CheckAngle()
 {
     Vector3 groundVector = Vector3.forward;
     Vector3 cameraVector = Camera.main.transform.forward;
     float angle = Vector3.Angle(groundVector, cameraVector);

     return angle;
 }
 float CheckDistance()
 {  
     float rayDist = 700.0f;
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

     if (Physics.Raycast(ray, out hit, rayDist))
     {      
         Debug.DrawRay(ray.origin, Camera.main.transform.forward * rayDist, Color.red);
         targetPoint = hit.point;
         distance = hit.distance;
   
         return distance;
     }
     else
     {
         return distance;
     }
    
 }

}

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 Firedan1176 · Dec 18, 2014 at 09:48 PM

What you could try is to get the velocity of the object you're shooting at and add that to the "target position" of your mechanism. So even if it's traveling away from the camera, it will still shoot at the correct location, but this implies that it takes exactly 1 second to reach the target no matter the distance.

I'm surprised it wasn't easy to find the answer, but a simple formula to calculate trajectory with the projectile's speed and the distance from the target:

 float angle = (Mathf.Asin ((Physics.gravity.magnitude * distance) / (velocity * velocity)) / 2) * 100;

And you can incorporate this into a simple formula to find a target position.

 public float bulletSpeed;

 //This is purely for effect, just makes it rotate smooth
 public float rotationSpeed;


 //This is a custom script that calculates the velocity of an object.
 //Because MY "test enemy" didn't have a rigidbody, I used code to calculate vel.
 Vector3 enemyVelocity = enemy.GetComponent<Velocity>().velocity;

 //Get the distance to the target as a float
 float distance = Vector3.Distance (transform.position, enemy.transform.position);

 //Get the number in seconds to reach the target.
 float timeToReachTarget = (distance / bulletSpeed);

 //This will be what we rotate towards (the "invisible target")
 Vector3 targetPosition = (enemy.transform.position - transform.position);

 //We will add the "tracking" stuff to make it aim up and ahead
 targetPosition += (enemyVelocity * timeToReachTarget) + (Mathf.Asin((Physics.gravity.magnitude * dist) / (vel * vel)) / 2) * 100);

 //This is purely for effect, just makes it rotate smooth
 Quaternion tempRotation = Quaternion.LookRotation(pos1);
 transform.rotation = Quaternion.Lerp(transform.rotation, tempRotation, rotationSpeed);



Problems: If your enemy doesn't have a rigidbody, you need to use code to calculate. If your enemy has a rigidbody, you need to get it and change the "Vector3 enemyVelocity" to get that rigidbody.velocity(???). If your enemy moves randomly and not at a constant speed, it won't work well.

I hope this is what your wanting.

This is script I have made before and if it doesn't work for you, I will try to fix it. I can also include a link to the script I made if you want to look at that.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycasting 1000 times 1 Answer

Is there a limit on the distance of raycast? 2 Answers

Physics.SphereCastAll with triggers 1 Answer

Raycasting to the bottom side of a plane? 2 Answers

Physics.CapsuleCast Help 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