So im trying to make it so that if my player is above a terrain sample with a new height than the one he jumped on, he wont stop in midair. Here’s my code, its somewhere in the jump controls. what do i change to make this possible?
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float Speed;
public float JumpHeight;
public float JumpSpeed;
public bool Jumping;
public float PlayerFoot;
public float PlayerFootSet;
public float TerrainHeight;
public float TerrainHeightSet;
public float Sensitivity;
private float MouseXSet;
void Start () {
MouseXSet = 0;
Jumping = false;
transform.position = new Vector3 (0, TerrainHeight + transform.lossyScale.y / 2, 0);
}
void Update () {
PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);
// Translation controls]
if (Input.GetKey (KeyCode.W))
{
transform.position += transform.forward * Speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S))
{
transform.position -= transform.forward * Speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D))
{
transform.position += transform.right * Speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A))
{
transform.position -= transform.right * Speed * Time.deltaTime;
}
// Rotation controls
if (Input.mousePosition.x > MouseXSet)
{
transform.Rotate (new Vector3 (0, 1, 0) * (Input.mousePosition.x - MouseXSet) * Sensitivity * Time.deltaTime);
MouseXSet = Input.mousePosition.x;
}
if (Input.mousePosition.x < MouseXSet)
{
transform.Rotate (new Vector3 (0, -1, 0) * (-(Input.mousePosition.x - MouseXSet)) * Sensitivity * Time.deltaTime);
MouseXSet = Input.mousePosition.x;
}
// Jump controls
if (Input.GetKey (KeyCode.Space) && Jumping == false && PlayerFoot < TerrainHeight + 1)
{
Jumping = true;
TerrainHeightSet = TerrainHeight;
}
if (Input.GetKeyUp (KeyCode.Space))
{
Jumping = false;
}
if (Jumping == true)
{
if (PlayerFoot < TerrainHeightSet + JumpHeight * 10)
{
PlayerFootSet = PlayerFoot;
rigidbody.velocity = new Vector3 (0, (JumpSpeed * 10 / (TerrainHeightSet + JumpHeight * 10 - PlayerFoot)) * JumpSpeed * 3, 0);
} else {
Jumping = false;
}
}
if (Jumping == false)
{
if (PlayerFoot > TerrainHeight)
{
Debug.Log ("Here");
if (PlayerFootSet < TerrainHeight + JumpHeight * 10)
{
Debug.Log ("There");
rigidbody.velocity = new Vector3 (0, -(JumpSpeed * 10 / (TerrainHeight + JumpHeight * 10 - PlayerFootSet)) * JumpSpeed, 0);
}
} else {
rigidbody.velocity = new Vector3 (0, 0, 0);
}
}
// Keep player upright
if (PlayerFoot < TerrainHeight)
{
transform.position += Vector3.up * (TerrainHeight - PlayerFoot);
}
}
}