- Home /
Code debugging message but not fulfilling action commanded to do
Where it says "here". it actually prints it, meaning the condition is met, but the velocity is not changed. why?
// Jump controls
Debug.Log (rigidbody.velocity);
if (Input.GetKey (KeyCode.Space) && Jumping == false && transform.position.y - transform.lossyScale.y / 2 < Terrain.activeTerrain.SampleHeight (transform.position) + 1)
{
Jumping = true;
}
if (transform.position.y - transform.lossyScale.y / 2 >= Terrain.activeTerrain.SampleHeight (transform.position) + JumpHeight * 10)
{
Jumping = false;
}
if (Jumping == true)
{
rigidbody.velocity = new Vector3 (0, (JumpSpeed / ((Terrain.activeTerrain.SampleHeight (transform.position)) + JumpHeight * 10 - transform.position.y - transform.lossyScale.y / 2) * 16), 0);
}
if (Jumping == false)
{
if (transform.position.y - transform.lossyScale.y / 2 > Terrain.activeTerrain.SampleHeight (transform.position) + 1)
{
Debug.Log ("Here");
rigidbody.velocity = new Vector3 (0, -(JumpSpeed / ((Terrain.activeTerrain.SampleHeight (transform.position)) + JumpHeight * 10 - transform.position.y - transform.lossyScale.y / 2) * 16), 0);
} else {
rigidbody.velocity = new Vector3 (0, 0, 0);
}
}
Update:when the player gets to Terrain.activeTerrain.SampleHeight (transform.position) + JumpHeight * 10 he just lunges up and then goes a bit slower than before.
Put your debug in the codeblock after the velocity change under 'Here'.
Debug.Log ("Here");
rigidbody.velocity = new Vector3~/~
Debug.Log (rigidbody.velocity);
Do you really need the 'new' keyword when adjusting the velocity? This I don't know.
Edit: Docs say it is :)
Answer by awplays49 · Dec 21, 2014 at 04:40 PM
i dont but i like keeping consistent in the pattern of my coding, ocd :{
I have it working now, but when he falls he sometimes shoots through the floor and snaps back up. heres my code
if (Input.GetKey (KeyCode.Space) && PlayerFoot < TerrainHeight + 1)
{
Jumping = true;
}
if (Jumping == true)
{
if (PlayerFoot < TerrainHeight + JumpHeight * 10 - 1)
{
PlayerFootSet = PlayerFoot;
if (PlayerFoot < (TerrainHeight + JumpHeight * 10) / 4 * 3)
{
rigidbody.velocity = new Vector3 (0, (JumpSpeed / (TerrainHeight + JumpHeight * 10 - PlayerFoot)) * 32, 0);
} else {
rigidbody.velocity = new Vector3 (0, (JumpSpeed / (TerrainHeight + JumpHeight * 10 - PlayerFoot)) * 24, 0);
}
} else {
Jumping = false;
}
}
if (Input.GetKeyUp (KeyCode.Space))
{
Jumping = false;
}
if (Jumping == false && PlayerFoot > TerrainHeight + 1)
{
if (PlayerFoot < (TerrainHeight + JumpHeight * 10) / 4 * 3)
{
rigidbody.velocity = new Vector3 (0, -(JumpSpeed / (TerrainHeight + JumpHeight * 10 - PlayerFootSet)) * 32, 0);
} else {
rigidbody.velocity = new Vector3 (0, -(JumpSpeed / (TerrainHeight + JumpHeight * 10 - PlayerFootSet)) * 24, 0);
}
}
if (Jumping == false && PlayerFoot <= TerrainHeight + 1)
{
rigidbody.velocity = new Vector3 (0, 0, 0);
}
What what happening is he was going slightly over the jump height because i used if
Its still happening? @meat5000 i changed something and it just went back to what it was doing before
using UnityEngine;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
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 $$anonymous$$ouseXSet;
void Start () {
$$anonymous$$ouseXSet = 0;
Jumping = false;
}
void Update () {
PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);
// Translation controls
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W))
{
transform.position += transform.forward * Speed * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S))
{
transform.position -= transform.forward * Speed * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D))
{
transform.position += transform.right * Speed * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))
{
transform.position -= transform.right * Speed * Time.deltaTime;
}
// Rotation controls
if (Input.mousePosition.x > $$anonymous$$ouseXSet)
{
transform.Rotate (new Vector3 (0, 1, 0) * (Input.mousePosition.x - $$anonymous$$ouseXSet) * Sensitivity * Time.deltaTime);
$$anonymous$$ouseXSet = Input.mousePosition.x;
}
if (Input.mousePosition.x < $$anonymous$$ouseXSet)
{
transform.Rotate (new Vector3 (0, -1, 0) * (-(Input.mousePosition.x - $$anonymous$$ouseXSet)) * Sensitivity * Time.deltaTime);
$$anonymous$$ouseXSet = Input.mousePosition.x;
}
// Jump controls
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space) && PlayerFoot < TerrainHeight + 1)
{
Jumping = true;
TerrainHeightSet = TerrainHeight;
}
if (Jumping == true)
{
if (PlayerFoot < TerrainHeightSet + JumpHeight * 10)
{
PlayerFootSet = PlayerFoot;
rigidbody.velocity = new Vector3 (0, (JumpSpeed / (TerrainHeightSet + JumpHeight * 10 - PlayerFoot)) * 32, 0);
} else {
Jumping = false;
}
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space))
{
Jumping = false;
}
if (Jumping == false && PlayerFoot > TerrainHeightSet + 1)
{
if (PlayerFoot < TerrainHeight + JumpHeight * 10 - 5)
{
rigidbody.velocity = new Vector3 (0, -(JumpSpeed / (TerrainHeightSet + JumpHeight * 10 - PlayerFootSet)) * 32, 0);
}
}
// $$anonymous$$eep player upright
if (PlayerFoot < TerrainHeight)
{
transform.position += Vector3.up * 5 * Time.deltaTime;
}
}
}
Debug.log(Terrain.activeTerrain.SampleHeight(transform.position).ToString());
This may not be doing what you think it is.
I found out this condition
if (PlayerFoot < TerrainHeight + JumpHeight * 10 - 5)
if never met. the one it is inside, however, is. so now i know what the problem is but im not sure how to approach it. @meat5000
it should be met though, im not sure why it isn't. @meat5000