Problems with onCollision

Greetings, I have made several attempts but keep hitting the same issue. Either I cannot prevent multiple jumps or I cannot jump at all. Please look at my code and let me know if you see any errors. I have also attempted to move the Head and CardboardMain objects with the logic that they might be set off the ground and registering as onCollisionExit, therefore preventing any jump at all. I did this as I skipped the “configure for Gear VR” lessons.

If I add onFloor = true; under the PullTrigger, I can jump but multiple times as I am always onFloor. Thank you for any assistance.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Player : MonoBehaviour {

public Text gazeText;

public float jumpAngleInDegree;
public float jumpSpeed;    

private CardboardHead head;
private Rigidbody rb;
private bool onFloor;

// Use this for initialization
void Start () {
    Cardboard.SDK.OnTrigger += PullTrigger;
    head = GameObject.FindObjectOfType<CardboardHead>();
    rb = GetComponent<Rigidbody>();

}

private void PullTrigger()
{
    if (onFloor)
    {
        float jumpAngleInRadians = jumpAngleInDegree * Mathf.Deg2Rad;
        Vector3 projectedVector = Vector3.ProjectOnPlane(head.Gaze.direction, Vector3.up);
        Vector3 jumpVector = Vector3.RotateTowards(projectedVector, Vector3.up, jumpAngleInRadians, 0);
        rb.velocity = jumpVector * jumpSpeed;
    }

}

void onCollisionEnter(Collision collision)
{
    onFloor = true;
}

void onCollisionExit(Collision collision)
{
    onFloor = false;
}

// Update is called once per frame
void Update () {
    gazeText.text = head.Gaze.ToString();

}

}

I just took a different approach on this one. I’m just adding a simple check to see if the current velocity of the toad is 0. if it is, then jump. if not, then do nothing.

private void PullTrigger()
{
    if (rb.velocity.magnitude == 0) {
        Vector3 projectionVector = Vector3.ProjectOnPlane(head.Gaze.direction, Vector3.up);
        Vector3 jumpVector = Vector3.RotateTowards(projectionVector, Vector3.up, Mathf.Deg2Rad * jumpAngleDegree, 0);
        rb.velocity = jumpVector * jumpSpeed;
    }
}

Seems fine now. Not sure if it will fall short going forward.

Privacy & Terms