- Home /
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
}
}
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
BELLA !!!!!!!!!!!!!!!!!!!!!!! hey he's from Blois really ;-)
the fact is nobody on here knows any math :)
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
Your answer
Follow this Question
Related Questions
Parenting a player to a rigidbody platform 0 Answers
What's wrong with this script? 4 Answers
Child object not moving with parent 3 Answers
Getting the player to move with platform: Velocity, DistanceJoint, or...? 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers