So problems I beat my head against the wall to solve are now problems again. I cant jump, the controller is no longer grounded… i changed nothing. I am really thinking unity just isnt for me.
What am i missing where if the controller is grounded info, in the editor? in the code? what the hell is going on. why did this work and now it isnt?
here was the result from Debug.Log("Grounded value = " + groundedPlayer.ToString());
Grounded value = False
UnityEngine.Debug:Log (object)
RotateBase:Update () (at Assets/RotateBase.cs:54)
and Debug.Log(“Jump Condition true”); of course wont return anything until it starts working.
this object is the parent of a “head” also but I assume it has no effect. it was working and i dont know that i changed anything. I have had a busy week thus far with work but will give it some proper attention soon.
I made a new project to add elements one at a time until failure occurs…
project has one block on a larger block.
the “player” block with the script is on top and has a character controller added.
it is set slightly high to fall to the other block upon startup. this has always seemed to work and is still working.
script is on top “player” block
block on block code with jump function - working
code with movement added - fails
Im suspicious of the “0” in the xyz “y” position in line 78…
it feels like it should be a “null” not a zero?
-edit- i relpaced the zero “0” with playerVelocity.y it seems like im trying to define movement in the y axis more than once but that didnt work the debug result is still “false” and jump function doesnt work.
These lines that were added when it breaks are noted in code and their associated variables are noted.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestingGrounded : MonoBehaviour
{
// docs.unity3d.com/ScriptReference/CharacterController.Move.html
// variable to reference the character controller
CharacterController controller;
// vel values in xyz
private Vector3 playerVelocity;
// grounded true false
private bool groundedPlayer;
// movement speed
private float playerSpeed;
// target of jump height
private float jumpHeight = 1.0f;
// downward force
private float gravityValue = -9.8f;
// ********************************************************************************
// ********************************************************************************
// ADDING LINES BELOW BREAKS THE INTENDED FUNCTION - NOT GROUNDED
// Moving
private float xAxisValue;
private float zAxisValue;
// ********************************************************************************
// ********************************************************************************
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
// ********************************************************************************
// ********************************************************************************
// ADDING LINES BELOW BREAKS THE INTENDED FUNCTION - NOT GROUNDED
xAxisValue = Input.GetAxis("Horizontal") * playerSpeed;
zAxisValue = Input.GetAxis("Vertical") * playerSpeed;
// ********************************************************************************
// ********************************************************************************
// for testing -------------------------------------------------
Debug.Log("Grounded value = " + groundedPlayer.ToString());
// Ground Check, reset vel to 0, if not jumping - This isnt working. velocity just keeps getting higher in value (negative number)
groundedPlayer = controller.isGrounded;
Debug.Log(groundedPlayer); // -----------------------------------------------
//if (groundedPlayer && playerVelocity.y < 0f)
if (controller.isGrounded && playerVelocity.y < 0f)
{
playerVelocity.y = 0f;
}
Debug.Log(playerVelocity); // -----------------------------------------------
// Jump
if (Input.GetKeyDown(KeyCode.Keypad0) && controller.isGrounded)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
Debug.Log("Jump Condition true"); // ------------------------------------
}
// Falling back down
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
// ********************************************************************************
// ********************************************************************************
// ADDING LINES BELOW BREAKS THE INTENDED FUNCTION - NOT GROUNDED
// Moving
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 vright = transform.TransformDirection(Vector3.right);
Vector3 move = new Vector3(xAxisValue, 0, zAxisValue);
controller.Move(forward * Time.deltaTime * playerSpeed * zAxisValue);
controller.Move(vright * Time.deltaTime * playerSpeed * xAxisValue);
// ********************************************************************************
// ********************************************************************************
}
}