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 Posthuman-Wizard · Dec 27, 2012 at 10:24 PM · movementphysicsrigidbodyforcekinematic

Kinematics: Trying to get an object to land at a known point

Hello, everyone. I'm trying to toss an object so that it always lands at a determined point, regardless of where it starts initially, and always in the same amount of time. I am trying to use basic kinematics to achieve this, but the object is not reaching the point as I desire. Here is my code:

     float d = Vector3.Distance(destination,rb.transform.position); //Get distance between the current position and the destination
     float tsqr = timeToReach*timeToReach; //Get the time to reach the destination squared
     float m = rb.mass; //Get the mass of the object to be tossed
     float f = (float)(d/((.5)*tsqr))*m; //d = v*t + (1/2)at^2 substituting a for f/m and solving for f
     rb.AddForce (f * Vector3.forward); //Apply force to rigidbody

What might I be doing wrong? Any help would be greatly appreciated.

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
1
Best Answer

Answer by aldonaletto · Dec 28, 2012 at 12:17 AM

Supposing the equation is correct for what you want to do, you should apply a constant force to get the desired result - a single AddForce applies the force only during one physics cycle, which usually is 20mS (unless you change Fixed Timestep in the Time Manager). You should instead use AddForce in FixedUpdate to apply the constant force, or add a Constant Force component and set its force property to the calculated value.
But I suspect that this is not what you actually want: applying the calculated constant force accelerates an object from 0 to some velocity in such a way that the object crosses the specified distance in the desired time - but only if gravity is off: if gravity is on, the rigidbody also falls towards -Y (default gravity), crashing to the ground or reaching a point well below your target. If gravity is on, you must use a ballistic equation instead - but solving it to reach the target in a predefined time is a pain in the ass.
I created a function that calculates the initial direction/velocity to throw an object so that it lands at the target position, provided that the starting and target positions are approximately at the same heights (the function compensates for height differences that are small when compared to the horizontal distance). The time to reach the target can't be specified, but if this can help you, take a look at my answer in this question.

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 Posthuman-Wizard · Dec 28, 2012 at 06:50 PM 0
Share

Hi; thanks for the response. When trying to use your function, though, the target is always off; it always seems to land a little bit past the target each time. How might I go about fixing this? Also, if I would like to draw out the parabola/trajectory of the projectile's flight, how would I go about doing that? Thanks.

avatar image aldonaletto · Dec 29, 2012 at 07:02 AM 0
Share

The function tries to hit the target position, not the ground below it - if the target object is a too above the ground, the projectile will land after it. You must place the target object's center at such height that the projectile hits the ground and the target position at the same time.
You can use a LineRenderer to draw the trajectory: create an empty game object, add a LineRenderer and child it to the projectile, then add the script below to the projectile:

 using UnityEngine;
 using System.Collections;

 public class DrawTrajectory : $$anonymous$$onoBehaviour {

     public float interval = 0.5f;
     
     private LineRenderer lineRender;
     private int nVertex = 0;
     private Vector3 lastPos;
     
     void Start () {
         lineRender = GetComponentInChildren<LineRenderer>();
         lineRender.SetVertexCount(1);
         lineRender.SetPosition(0, transform.position);
         lastPos = transform.position;
     }
     
     void Update () {
         if (Vector3.Distance(transform.position, lastPos) > interval){
             lineRender.SetVertexCount(nVertex+1);
             lineRender.SetPosition(nVertex, transform.position);
             lastPos = transform.position;
             nVertex++;
         }
     }
 }

The projectile draws its trajectory each interval distance. If you want to destroy the projectile but still keep it's trajectory, unchild the trajectory object before destroying the projectile.

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

10 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

Related Questions

Rigid body child Player movement 0 Answers

Erratics movements from my rigidbody : why does it behave like that? 0 Answers

Simple Movement Game: Physics vs Manual Collision Detection? 2 Answers

Moving a kinematic object a fixed distance over a set time with physics in mind 2 Answers

Predicting land position with drag applied 0 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