- Home /
Strange happenings with my code
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);
}
}
}
if my player is above a terrain sample with a new height than the one he jumped on, he wont stop in midair
I don't quite understand what you're trying to do. what exactly are you having trouble with?
And what exactly is going wrong?
are you trying to ground a character without using gravity after jumping from a higher point i.e a ledge
There is some effort goon on behind the scenes to improve the site. Tags are one of the areas we might get cleaned up. In the meantime am asking mods to try and push it. So spread the word.
@Fornoreason1000 exactly. im trying to make it so that the grounding code applies to whenever he is not jumping. When i tried to make it so that it would also work when he jumps and moves, he stopped in midair. This is because of something in The (if false) jump code. There is a terrain height set so if the player moves uphill, he cant keep floating. Only when he is near grounded will the terrain height set be set.
Answer by Mmmpies · Dec 22, 2014 at 04:54 PM
Record the start height (when space is first pressed) and set the maximum jump height to startHeight + jumpHeight. Let that control how high your character can jump rather than the distance from the ground.
Use the terrain height to detect when the jump ends.
If Space is pressed and Jumping == True the you should do nothing handle the jump code in a jumping function and check if(Jumping) in Update to call that function if Jumping == True.
Looks like you're OK at coding but give me a nudge if you want a hand implementing.
If you code like this then even if your character jumps off a cliff they should still fall as they only ever get up to startHeight + jumpHeight.
EDIT:
Well you're setting Jumping to false when you let go of the button. That isn't really true is, the character is still jumping until they land.
It depends on how you want the game to work, do you want the character to jump to the maximum height no matter how long space is pressed or do you want to only keep going up if space is being pressed?
That'll affect how we deal with the code.
Ok thanks :-)
so this is my code now, the only problem left is he still floats up if he moves over a lower terrain while jumping
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space) && Jumping == false && PlayerFoot < TerrainHeight + 1)
{
Jumping = true;
TerrainHeightSet = TerrainHeight;
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.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 + 1)
{
if (PlayerFootSet < TerrainHeight + JumpHeight * 10 && PlayerFoot >= PlayerFootSet)
{
Debug.Log ("1");
rigidbody.velocity = new Vector3 (0, -(JumpSpeed * 10 / (TerrainHeight + JumpHeight * 10 - PlayerFootSet)) * JumpSpeed, 0);
}
if (PlayerFoot < PlayerFootSet)
{
Debug.Log ("2");
rigidbody.velocity = new Vector3 (0, -(JumpSpeed * 10 / (TerrainHeight + JumpHeight * 10 - PlayerFoot)) * JumpSpeed, 0);
} else {
rigidbody.velocity = new Vector3 (0, 0, 0);
}
}
the condition with the "2" in it isnt met when jumping toward a lower surface apparently
Got it working?
If so it's good practice to post the working script and mark the question answered, could help someone else. Could even help you if your project screws up for any reason and you need to get the code back.
its something in the condition that changes the force to move down, though. either something in the jumping == true part or the jumping == false part. @meat5000
Your answer
Follow this Question
Related Questions
how do i delete a stupid question? 1 Answer
Why doesn't Unity Answers work in Firefox? 2 Answers
Attempt at re-arranging list results in crash 1 Answer
I cant shoot the ammo when i trigger the box 1 Answer
Orbiting bodies with unity 5.0 2 Answers