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 Ekta-Mehta-D · Dec 04, 2013 at 10:13 AM · linerendererprojectiletrajectory

Ball Isn't following Trajectory line : Unity3d

Hello Everyone,

I am working on projectile motion. Sometimes my ball does not follow the trajectory line.

Code is here :

 #pragma strict
 #pragma implicit
 #pragma downcast 
 var turret : Transform; 
 var bulletPrefab : Transform;
 private var plane : Plane;
 private var power : float = 30;
 private var gravity: float = 9.8;
 public var distance : float = 1.0f;
 public var  predictionLine : LineRenderer;
 public var enablePrediction : boolean = true;
 public var targetPoint : boolean = true;
 var velocity : Vector3;
 
 
 private var horizontalOffset : float = 0.0f;
 private var  lob : float = 0.75f;
 var bullet : Transform ;
     
 function Awake ()
 {
     plane = Plane(-Vector3.forward,turret.position);
     predictionLine.SetWidth(1.0f,1.0f);
     bullet = Instantiate(bulletPrefab, turret.position, turret.rotation);
 }    
 function Update () 
 { 
     var distanceToPlane : float;
     var hit : RaycastHit; 
     var ray = camera.ScreenPointToRay(Input.mousePosition);   
     var hitPoint : Vector3;
     
     if(plane.Raycast(ray,distanceToPlane))
     {
             hitPoint = ray.GetPoint(distanceToPlane);
             velocity = GetTrajectoryVelocity(turret.position, hitPoint, lob, Physics.gravity);    
             turret.up = velocity;
     }
     
 //    if(Input.GetMouseButtonDown(0))
 //    {
 //        if(plane.Raycast(ray,distanceToPlane))
 //        {
 //            GameObject.Find("SoccerBall(Clone)").transform.position = turret.position;
 //            FireProjectile();
 //        }
 //    } 
     
 
     horizontalOffset = Mathf.Clamp(horizontalOffset, -45.0f, 35.0f);
         
     lob += Input.GetAxis("Mouse ScrollWheel") * 0.01f;
     //Debug.Log("lob" + lob);
     lob = Mathf.Clamp(lob, 0.25f, 1.2f);
     
     distance = Mathf.Clamp(distance, 0.25f, 1.5f);
 
     UpdatePredictionLine();  
         
     if (Input.touchCount > 0) {
         var touch = Input.GetTouch(0);
         
         switch (touch.phase) {
         
         case TouchPhase.Began:
             UpdatePredictionLine();  
             break;
         
         case TouchPhase.Moved:
             UpdatePredictionLine();  
             break;
             
         case TouchPhase.Ended:
             GameObject.Find("SoccerBall(Clone)").transform.position = turret.position;
             UpdatePredictionLine();  
             FireProjectile();
             break;
            }
         }
     }
 
     
         function UpdatePredictionLine() 
         {
             predictionLine.SetVertexCount(180);
             var previousPosition : Vector3 = turret.position;
             for(var i : int = 0; i < 180; i++)
             {
                 var currentPosition : Vector3 = GetTrajectoryPoint(turret.position, velocity, i, 1, Physics.gravity);
                 var direction : Vector3= currentPosition - previousPosition;
                 direction.Normalize();
                 
                 var distance : float = Vector3.Distance(currentPosition, previousPosition);
                 
                 var hitInfo : RaycastHit ;
                 if(Physics.Raycast(previousPosition, direction,  hitInfo, distance))
                 {
                     predictionLine.SetPosition(i,hitInfo.point);
                     predictionLine.SetVertexCount(i);
                     break;
                 }
                 
                 previousPosition = currentPosition;
                 predictionLine.SetPosition(i,currentPosition);
             }
         }
         
         function GetTrajectoryPoint( startingPosition : Vector3 ,  initialVelocity : Vector3 ,  timestep : float,  lob : float ,  gravity : Vector3) : Vector3
         {
             var physicsTimestep : float = Time.fixedDeltaTime;
             var stepVelocity : Vector3 = initialVelocity * physicsTimestep;
             
             //Gravity is already in meters per second, so we need meters per second per second
             var stepGravity : Vector3 = gravity * physicsTimestep * physicsTimestep;
             
             return startingPosition + (timestep * stepVelocity) + ((( timestep * timestep + timestep) * stepGravity ) / 2.0f);
         }
         
         function GetTrajectoryVelocity( startingPosition : Vector3,  targetPosition : Vector3,  lob : float ,  gravity : Vector3) : Vector3
         {
             var physicsTimestep : float = Time.fixedDeltaTime;
             var timestepsPerSecond : float = Mathf.Ceil(1f/physicsTimestep);
             
             //By default we set n so our projectile will reach our target point in 1 second
             var n : float = lob * timestepsPerSecond;
             
             var a : Vector3 = physicsTimestep * physicsTimestep * gravity;
             var p : Vector3 = targetPosition;
             var s : Vector3 = startingPosition;
             
             var velocity : Vector3 = (s + (((n * n + n) * a) / 2f) - p) * -1 / n;
             //This will give us velocity per timestep. The physics engine expects velocity in terms of meters per second
             velocity /= physicsTimestep;
             return velocity;
         }
         
         function FireProjectile() {
             var rb : Rigidbody = GameObject.Find("SoccerBall(Clone)").GetComponent(Rigidbody);
             rb.useGravity = true;
             rb.transform.position = turret.position;
             
             rb.AddForce(velocity, ForceMode.Impulse);
         } 

So what can be the reason? While touching it is changing the velocity?? I could not identify the bug.

It is specially happening with large angle.

Thanks in advance for your help and support....

device-2013-12-04-152801.png (478.1 kB)
Comment
Add comment · Show 4
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 hirenkacha · Dec 04, 2013 at 10:29 AM 0
Share

In editor, use

 Input.Get$$anonymous$$eyDown($$anonymous$$eycode.$$anonymous$$ouse0) for TouchBegan
 Input.Get$$anonymous$$ey($$anonymous$$eycode.$$anonymous$$ouse0) for Touch$$anonymous$$ove
 Input.Get$$anonymous$$eyUp($$anonymous$$eycode.$$anonymous$$ouse0) for TouchEnd

and get the Logs for the values. You will find the problem on your own. And these Get$$anonymous$$ey methods will work same on devices too. No need to write separate code for touch events.

For quick response come to chatroom

avatar image Ekta-Mehta-D · Dec 04, 2013 at 12:41 PM 0
Share

I have edited my question. It is behaving same way for mouse and touch input.

avatar image Ekta-Mehta-D · Dec 04, 2013 at 01:01 PM 0
Share

Still could not able to find the solution. Rigidbody force is not working for large angle.

avatar image hirenkacha · Dec 04, 2013 at 05:30 PM 0
Share

why dont you try iTween for curved path??

1 Reply

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

Answer by hirenkacha · Dec 05, 2013 at 08:32 AM

You need to reset the velocity to zero before firing the bullet. For firing the bullet inside your FireProjectile() just add one line like following

 function FireProjectile() 
     {
         var rb : Rigidbody = GameObject.Find("Ball(Clone)").GetComponent(Rigidbody);
         rb.useGravity = true;
         rb.velocity=Vector2.zero;  //<=========== ADD THIS
         rb.transform.position = turret.position;
         rb.AddForce(velocity, ForceMode.Impulse);
     } 

Also for PC you can use this functions which will work on android / ios too.

                 if(Input.GetKeyDown(KeyCode.Mouse0))
                 {
                      UpdatePredictionLine();
                 }
                 if(Input.GetKey(KeyCode.Mouse0))
                 {
                      UpdatePredictionLine();
                 }
                 if(Input.GetKeyUp(KeyCode.Mouse0))
                 {
                     GameObject.Find("Ball(Clone)").transform.position = turret.position;
                     UpdatePredictionLine();
                     FireProjectile();
                 }
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

17 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

Related Questions

Unity Messing up Basic Math? Projectile Trajectory Algorythm 1 Answer

A node in a childnode? 1 Answer

Making a trail renderer keep it's form of arc when object moves (Keep it in local space) -1 Answers

Trajectory Projectile with Collision Detection 2 Answers

Setting limits to my trajectory 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