- Home /
Ground and Object Jumping problem
Hey guys, Here is my problem. My Jumping works fine, however, lets say I jump when I am literally against the wall, as the photo shows. Like literally facing the wall and against it and press jump. Well it's disabling my canJump Bool and won't reactivate unless I climb up and object and fall off it and hit the ground again. No matter what order I put it in.
Is there something I am missing here?
I'll post both the player code and the Object Detection code.
PLAYER CODE.
using UnityEngine;
using System.Collections;
// REQUIRED COMPONENTS TO WORK!
[RequireComponent(typeof (Animator))]
public class Controller : MonoBehaviour {
private CapsuleCollider col; // This is the Collider for Player.
private Animator anim; // This is the Animator for Player.
public float Speed = 5.0f;
public float TurnSpeed = 5.0f;
public float jumpHeight = 10.0f;
public bool canJump;
public bool grounded;
public bool ObjDetected = false;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent<Animator>();
grounded = true;
}
// Update is called once per frame
void Update () {
//if (grounded) {
if (Input.GetKey (KeyCode.W)) {
transform.Translate (Vector3.forward * Speed * Time.deltaTime);
anim.SetFloat ("Speed", 1f);
} else {
anim.SetFloat ("Speed", 0f);
}
if (Input.GetKey (KeyCode.S)){
transform.Translate (Vector3.back * Speed * Time.deltaTime);
anim.SetFloat ("WalkBack", -1f);
} else {
anim.SetFloat ("WalkBack", 0f);
}
if(Input.GetKey (KeyCode.A)){
transform.Rotate (0,-5,0);
}
if(Input.GetKey (KeyCode.D)){
transform.Rotate (0,5,0);
}
if (ObjDetected) {
transform.Translate(Vector3.back * 5 * Time.deltaTime);
}
//}
}
void FixedUpdate(){
if (Input.GetKeyDown (KeyCode.Space) && canJump == true) {
if (canJump) {
transform.rigidbody.AddForce (Vector3.up * jumpHeight);
Physics.gravity = new Vector3 (0, -20, 0);
canJump = false;
} else {
canJump = true;
}
}
}
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "Map") {
canJump = true;
}
}
}
OBJECT DETECTION CODE (IT'S A BOX TRIGGER)
using UnityEngine;
using System.Collections;
public class ObjectDetection : MonoBehaviour {
public Controller controller;
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "LvlObject" && Input.GetKey (KeyCode.Space)) {
Debug.Log ("Object Detected");
controller.ObjDetected = true;
}
}
void OnTriggerExit(Collider col){
if (col.gameObject.tag == "LvlObject") {
Debug.Log ("Object no longer detected");
controller.ObjDetected = false;
}
}
}
Can you please help me figure out why this happens or at least guide me to the right path? I would do a raycast, but it got confusing with how to exactly implement it, as I've never really used Rays before.
EDIT - Figured I'd add as a comment as it would be easier to read.
I forgot to mention I pretty much created a little useless hack around to make it kind of work.
I had it just reactivate the canJump bool when I left the trigger. However that's not exactly what I am looking for.. I'm wanting to still jump when I hit the ground regardless, not when I leave the trigger.
Answer by sevensixtytwo · Oct 03, 2014 at 02:39 AM
Try using OnTriggerStay to set your canJump bool to true as long as it's inside the trigger and then setting the bool to false at the end of every frame using LateUpdate.
This is how, IIRC, the RigidbodyFPSWalker script did it in the unity wiki.
Thank you!
I just did what you put and it worked!
Thank you very much for your help!
Your answer
Follow this Question
Related Questions
Graceful 2D collision enabling/disabling for topdown jumping 1 Answer
Character Movement and Jumping Script? 4 Answers
Jumping boolean check 2 Answers