- Home /
knowing maximum lenght a player can reach
Hello all , I am making a 2D endless runner game where platforms appear randomly ahead of player. These platforms can take random x and y positions and z=0. However , I need to write a formula that will enable me to let those platforms generate within the reach of the player. The code below describes the movement of the player. to move the player left and right , rigidbody.velocity was used. to move the player up , rigidbody.AddForce was used. A combination of both can let the player jump left or jump right My question is , how can I know what is the maximum lenght the player can reach ? This will help me set the maximum length between the platforms (in height and width). any extra explications or physics figures and schemes will be really much appreciated. Thank you for all in advance.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour {
public float MaxSpeed;
private Rigidbody2D Rigid;
public bool OnGround = false;
public float CheckGroundRadius;
public Transform GroundPoint;
public float JumpHeight;
public LayerMask GroundLayer;
public Button JumpButton;
// Use this for initialization
void Start () {
Rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
Debug.DrawLine(GroundPoint.position,new Vector3(GroundPoint.position.x,GroundPoint.position.y + CheckGroundRadius,GroundPoint.position.z));
if(OnGround && Input.GetAxis("Jump")>0)
{
OnGround = false;
Rigid.AddForce(new Vector2(0, JumpHeight));
}
}
void FixedUpdate()
{
OnGround = Physics2D.OverlapCircle(GroundPoint.position, CheckGroundRadius, GroundLayer);
float Move = Input.GetAxis("Horizontal");
Rigid.velocity = new Vector2(Move * MaxSpeed, Rigid.velocity.y);
}
public void MoveLeft()
{
Rigid.velocity = new Vector2(-MaxSpeed, Rigid.velocity.y);
}
public void MoveRight()
{
Rigid.velocity = new Vector2(MaxSpeed, Rigid.velocity.y);
}
public void Jump()
{
if (OnGround)
{
OnGround = false;
Rigid.AddForce(new Vector2(0, JumpHeight));
}
}
}
Ins$$anonymous$$d of basing it off from the player, base it off the existing platforms. $$anonymous$$now how far your player can jump etc and make an algorithm that can handle this.
Your answer
Follow this Question
Related Questions
Touch and move in any axis 0 Answers
How can I set a timer? 1 Answer
Jump problem help 0 Answers
Moving Walls 1 Answer
Jumping with Character Controller?! 1 Answer