I am attempting to put a few videos worth of code together on one script and have run into a problem. I have a cube that I am able to move, jump, and charged jump with. I created an animation for the charged jump and after adding it to the script, am now unable to move, or jump. The animation happens (the cube “crouches”), but when I release the jump, it does not leave the ground. I have seen comments in the video about his “logic” being a bit unsound. If anyone sees this and could take a look at my script, I would greatly appreciate it!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeMove : MonoBehaviour {
private bool onGround;
private float jumpPressure;
private float minJump;
private float maxJumpPressure;
private Rigidbody rbody;
private Animator anim;
public float moveSpeed;
// Use this for initialization
void Start ()
{
moveSpeed = 10f;
onGround = true;
jumpPressure = 0f;
minJump = 2f;
maxJumpPressure = 10f;
rbody = GetComponent<Rigidbody> ();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
{
if (onGround)
{
//holding jump button//
if (Input.GetButton ("Jump"))
{
if (jumpPressure < maxJumpPressure)
{
jumpPressure += Time.deltaTime * 10f;
}
else
{
jumpPressure = maxJumpPressure;
}
anim.SetFloat ("jumpPressure", jumpPressure + minJump);
}
//not holding jump button//
else
{
//jump//
if (jumpPressure > 0f)
{
jumpPressure = jumpPressure + minJump;
rbody.velocity = new Vector3 (jumpPressure / 10, jumpPressure, 0f);
jumpPressure = 0f;
onGround = false;
anim.SetFloat ("jumpPressure",0f);
anim.SetBool ("onGround",onGround);
}
}
}
}
}
void OnCollisionEnter(Collision other)
{
if(other.gameObject.CompareTag ("ground"))
{
onGround = true;
anim.SetBool ("onGround", onGround);
}
}
}