Hey all, I’m on day two of coding in Unity without tutorials so my apologies if this question seems a bit noobish. I’ ve got my player object double jumping well enough but I’d like to have the ‘if (Input.GetKeyDown (“space”)’ statement in update and rest of the code for jumping in FixedUpdate. I’ve tried using a bool that would switch when the spacebar is pressed, which would then trigger everything jump realted in FixedUpdate but that just broke the jumping all together (no doubt I coded it bass ackwards and killed it myself.) so I reverted it back to when the jump worked. Any advice you all could give me to have it run as it should would be much appreciated?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 12;
public float jumpspeed = 1;
public float collected = 0;
public bool isDoorOpen = false;
public bool isGrounded = true;
public bool canDoubleJump = true;
public GameObject coin;
Rigidbody rb;
// Use this for initialization
void Start () {
print(collected);
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Vector3 velocity = movement.normalized;
rb.AddForce(velocity * speed);
if (isGrounded == true)
{
if (Input.GetKeyDown("space"))
{
Vector3 jump = new Vector3(0, 1, 0);
rb.velocity = jump * jumpspeed;
}
}
else if ((isGrounded == false) && (canDoubleJump == true))
{
if (Input.GetKeyDown("space"))
{
Vector3 jump = new Vector3(0, 1, 0);
rb.velocity = jump * jumpspeed;
canDoubleJump = false;
}
}
if (collected == 12)
{
isDoorOpen = true;
collected = 0;
}
}
void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.tag == "floor")
{
isGrounded = true;
canDoubleJump = true;
}
}
void OnCollisionExit(Collision theCollision)
{
if (theCollision.gameObject.tag == "floor")
{
isGrounded = false;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
collected += 1;
print(collected);
}
}
}