• 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
2
Question by corax · Sep 20, 2012 at 10:53 AM · rigidbodyplatform

Rigidbody jump on platform

I'm trying to use rigidbody to make my character jump on a specific platform. As you can see in the image, the character can be on the ground and has as goal to jump on a platform or can be on a ledge and has as goal to jum on a platform (below o above the character position). Is there a way to calculate the right force to jump on the platform center? I'm using this code but the character jumps too far!

Edit: After 10 days I'm still stuck with this part of the code. . .Now I'm able to jump with a fixed angle and land on the specific spot but cant calculate the best angle for that jump. Here is my code:

 void FixedUpdate() 
     {
         OnGround();
         
         if(Input.GetButtonDown("Fire1"))
     {
         Ray ray  = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
             
             
             
         if(Physics.Raycast(ray, out hit) && isOnGround)
         {
                 
             
             if(hit.collider.CompareTag("Respawn"))
             {
                 GameObject statLedge = Instantiate(ledge,hit.point,Quaternion.identity) as GameObject;
                 statLedge.transform.rotation = Quaternion.FromToRotation(Vector3.right, hit.normal);
                 
                 
             }
             
             if( hit.collider.CompareTag("ledge") || hit.collider.CompareTag("powerup"))
             {
                 biped.CanMove = false;
                 targetPoint = hit.collider.bounds.center;
                 transform.LookAt(new Vector3(targetPoint.x,transform.position.y,targetPoint.z));
                 CheckTrajectory(targetPoint);
                 rigidbody.velocity = JumpVelocity;
             }
 
             
             if(hit.collider.CompareTag("Finish"))
             {
                 biped.CanMove = true;
                 steerPoint.TargetPoint = hit.point;
             }
             
             
             
             
                 }
         
 
         }
 
  float CalculateAngle(Vector3 target)
     {
         Vector3 relative = transform.InverseTransformPoint(target);
         float a = Mathf.Atan2(relative.y,relative.z);
         float aDeg = Mathf.Rad2Deg * a;
         //Debug.Log(aDeg);
         return aDeg;
     }
     
   
     
     Vector3 BallisticVel(Vector3 target, float angle) 
     {
         float vel  = 0;
         dir = target - transform.position;  // get target direction
         float h = dir.y;  // get height difference
         dir.y = 0;  // retain only the horizontal direction
         float dist = dir.magnitude ;  // get horizontal distance
         float a = angle * Mathf.Deg2Rad;  // convert angle to radian
         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
         vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
         return vel * dir.normalized;
 
     }
     
     
     Vector3 TrajectoryPoint(Vector3 startingPosition, Vector3 startingVelocity, float t )
     {
         Vector3 point;
         
         float tSec = t/60f;
         point.x = startingVelocity.x * tSec + startingPosition.x;
         point.z = startingVelocity.z * tSec + startingPosition.z;
         point.y = -Physics.gravity.y * ((tSec*tSec) / 2) + (startingVelocity.y * tSec) + startingPosition.y;
         
         return point;
     }
     
     Vector3 VerticalDistance(Vector3 target)
     {
         
         Vector3 verticalVelocity = Vector3.zero;

float dist = target - transform.position.y; verticalVelocity.y = Mathf(Physics.gravity.y Physics.gravity.y dist); return verticalVelocity; }

     void CheckTrajectory(Vector3 target)
     {
         Debug.Log("checking");
             
         int counter = 0;
         loopEnd = false;
         angleJump = CalculateAngle(target);
         
         if(angleJump < 85)
         {
             
             while(!loopEnd  && counter < 60 && angleJump < 85f)
             {
             
             Scan(target);
             JumpVelocity = BallisticVel(target,angleJump);
             counter++;
             }
         }
         
         if(angleJump >= 85f)
         {
             JumpVelocity = VerticalDistance(target);
         }
         
         if (angleJump < 15f)
         
         {
             JumpVelocity = BallisticVel(new Vector3(target.x,transform.position.y,target.z),15f);
         }
         
         if(angleJump == 0)
         {
             //walk
         }
 
         
     }

alt text

ledge.jpg (36.5 kB)
Comment
Add comment · Show 18
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 Fattie · Sep 20, 2012 at 02:49 PM 1
Share

unfrtunately the answer is the same as this question !!

http://answers.unity3d.com/questions/320466/how-do-i-calculate-the-angle-to-fire-a-bullet-to-i.html

you will get 8 million answers in ten $$anonymous$$utes on stackexchange, good luck

avatar image Fattie · Sep 20, 2012 at 02:50 PM 2
Share

awesome diagram !!

avatar image corax · Sep 20, 2012 at 02:55 PM 0
Share

I'm italian that's a Leonardo Da Vinci diagram! ahahahah xD

avatar image Fattie · Sep 20, 2012 at 03:00 PM 1
Share

BELLA !!!!!!!!!!!!!!!!!!!!!!! hey he's from Blois really ;-)

the fact is nobody on here knows any math :)

avatar image corax · Sep 20, 2012 at 03:03 PM 0
Share

ahahaha, yup. This is the kind of math that you must study in underschool, but, you know, in my school there was so many girls so. . . xD

avatar image Fattie · Sep 20, 2012 at 03:34 PM 1
Share

L O L ... firenze, asti, italia !!

avatar image AlucardJay · Sep 20, 2012 at 03:38 PM 1
Share

Aldo has all the answers for this type of thing :

http://answers.unity3d.com/questions/148399/shooting-a-cannonball.html

http://answers.unity3d.com/questions/145972/how-to-make-enemy-canon-ball-fall-on-mooving-targe.html

http://answers.unity3d.com/questions/296749/display-arc-for-cannons-ball-trajectory.html

avatar image Fattie · Sep 20, 2012 at 03:51 PM 0
Share

@alu ... this is actually a pretty difficult problem because you are shooting to a DIFFERENT height. sucks!

avatar image corax · Sep 20, 2012 at 03:56 PM 0
Share

Yep when you start the game there are no platform at all! the player will create specific platform to reach the goal :D If the platform are too far from the character the charater will fall, If not we will succeed in landing on the platform :D

avatar image AlucardJay · Sep 20, 2012 at 04:00 PM 0
Share

Oh well, it was the closest thing as a resource I had to a Unity related rigidbody trajectory question. Now I am curious, have fond memories of Worms3D.

avatar image corax · Sep 24, 2012 at 11:45 AM 0
Share

This gonna give me an headache. I'm trying but I think that we are very far from the solution. $$anonymous$$y problem is inverse compared to the one in the links. I have a fixed velocity(my char velocity), a fixed jump force (hey the guy can jump very high but has his limits!) and a fixed landing spot(the one I want the char to land) but I must find if I'm in the right spot to jump(the jump is achievable, there are no obstable and my the char jump force can land in the right spot) then I must apply the right xforce and zforce (yforce is fixed remember? ) to steer the guy on the right spot. Any guess?

avatar image Fattie · Sep 24, 2012 at 01:48 PM 1
Share

man, you should defniyely ask on math.stackexchange .. now that you know know fixed velocity, fixed etc etc ... they will give you an answer in 10 $$anonymous$$s! mathematicians are great. very pretentious but you get an answer. :O

avatar image corax · Sep 24, 2012 at 01:50 PM 0
Share

$$anonymous$$an I designed the SI$$anonymous$$PLEST game on the planet earth because I don't want to mess with high mathematics. . . guess what, here we are -.-

avatar image corax · Sep 26, 2012 at 09:52 AM 0
Share

updated the code!

avatar image corax · Oct 01, 2012 at 12:23 PM 0
Share

New update of the code. I'm close, very close to the solution but still not there

avatar image AlucardJay · Oct 03, 2012 at 02:32 AM 0
Share

This new answer just came up (again from @aldonaletto, what a genius!), it seems to cover different elevations to a degree. I suggest experiment in a new scene/project to see if anything there is applicable : http://answers.unity3d.com/questions/248788/calculating-ball-trajectory-in-full-3d-world.html

avatar image Ampler Games · Oct 03, 2012 at 03:24 AM 0
Share

Love your diagram :D

avatar image corax · Oct 03, 2012 at 09:16 AM 0
Share

Last edit I hope, solve almost everything(need some tweakings). And thanks about the diagram, I was wondering if I must start a new carrier as comics author :D

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Something on a moving platform 2 Answers

Rigidbody slides across surface 0 Answers

Player slips off moving platform if Animator Component enabled 0 Answers

Limiting and Rotating a Platform. 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