• 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 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 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

Show more comments

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

Character Slowly sliding off platform 1 Answer

How to use rigidbody.MovePosition within OnTrigger? 0 Answers

Spinning rigidbody platform in 2D 0 Answers

Again about the platforms... 0 Answers

Platform and RigidyBody 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