Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
Anonymous's avatar
Anonymous
11 years ago

Unity : OK to jump but impossible to bounce

Hi,

here a piece of code that make my character jump with gravity. It works only if i push the J key. Impossible to make him jump automatically when he touch the ground. I try everything with no success.For example, i try a moveDirection.y = jumpSpeed when the character collide with my cube but nothing append. In the update function, i try to delete the if condition by pressing the J key with only moveDirection.y = jumpSpeed; : the character jump only one time and stay at the ground.

Thanks in advance !

var speed: float = 5.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;


function Update() {



var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {

// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;


if (Input.GetKey(KeyCode.J)) {
moveDirection.y = jumpSpeed;

}



}



// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}


function OnControllerColliderHit (hit : ControllerColliderHit){
if(hit.gameObject.name == "Cube"){
moveDirection.y = jumpSpeed;
} }

8 Replies

Replies have been turned off for this discussion
  • Hi, why not either actually call the OnControllerColliderHit instead, or even maybe easier (i haven't tested this but should work!) use this instead of the j key to jump:

    function Update() {



    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded)
    {
    moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;
    moveDirection.y = jumpSpeed;
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
    }


    As i have said it should work and is much less code than writing a function i cannot see being called anyway!
    Try it out and return here if anything should fail :)

    Edit!
    I have just tried this code in an entirely new project since i normally use C# and not JavaScript :) But i can confirm that you can use this code to repeatedly make a character bounce! but IF you're gonna go with the other function in your script, then remember to actually call it ;) Since it is a custom function it will NOT be called automatically ;)

    Edit2! Ok so i found out that you have a function that is not self-made, but it would be a lot easier for yourself if you used OnTriggerEnter instead with the following code:

    function OnTriggerEnter (other : Collider)
    {
    if(other.name == "Cube")
    {
    moveDirection.y = jumpSpeed;
    }
    }


    You can change the .name to .tag if you prefer, but notice that you will need an extra object (in my case to make this work i just duplicated the original Cube and placed it above the first one, set the mesh renderer to disabled so it is an invisible object! but that added to your current script will make the character bounce whenever the character is in touch with that Collider ;) Also notice the collider it should collide with must have "isTrigger" enabled ;)) I hope you can use this for something :P But simply overwriting the original OnControllerColliderHit with the code above should do the trick :D

    Also worth noting, with this OnTriggerEnter function, you need to actually jump inside the Triggered collider in order to "bounce" ;) and as soon as you leave the collider area, the player get back on ground like normal... was that the thing you wanted or am i missing something here??? anyway i would like to point out that i have never heard of that function you use currently, so therefore i thought it was a self-made function which wasn't being called :P sorry about that ;) Cheers.
  • Anonymous's avatar
    Anonymous
    Hi,

    first of fall, thanks for all your explanations !

    unfortunately my problem is not resolve but thanks to you I know where it comes from.

    So i make a new project with simply a plane and a cube. I attached your jump script (which is better from mine, thanks !) and the cube bounces well.

    The problem is that i attached the jump script to the OVRPlayercontroller (oculus prefabs camera and player controller), not to and object like a cube. It worked with SDK 0.4.2 but with new oculus prefabs in 0.4.3 (OVR Camera Rig), it dosen't work...

    Thanks to you, i know now where the problem comes from. Have you an idea ? Perhaps i should not attached the script to OVRplayercontroller ? But i am a big newbie, javascript and unity were unknow for me two months ago :D

    Thanks again !

    PS : my jump script is used in my very firsy mini demo available on oculus share : trampolake. try it if you have 2 minutes ;)

    Bye
  • Hi thebko, alright so i have remade the script though now it is in C# (hope that doesn't matter :P) so first of all you need to change the following script-lines in OVRPlayerController.cs :

    change
    protected CharacterController 	Controller 		 = null;


    to
    public CharacterController 	Controller 		 = null;


    and if the MoveThrottle variable line is not public then change it to:

    public Vector3 MoveThrottle   = Vector3.zero;


    then you need to create a script that is called playerBounce.cs (note it must be a C# script or else you'll get a lot of errors :( ) and paste these lines of code into the playerBounce.cs (you need to overwrite everything that is in the newly created script!)

    using UnityEngine;
    using System.Collections;

    public class playerBounce : MonoBehaviour
    {
    private bool isGrounded;
    private float counter = 0f;
    private OVRPlayerController thisObject;

    void Awake ()
    {
    thisObject = GetComponent<OVRPlayerController>();
    }

    void Update ()
    {
    RaycastHit hit;

    if (Physics.Raycast(transform.position, -Vector3.up, out hit, 1000f))
    {
    if (hit.collider.CompareTag("Ground") && !thisObject.Controller.isGrounded)
    {
    isGrounded = false;
    counter -= Time.deltaTime;
    if (!isGrounded)
    {
    if (counter > 0)
    thisObject.MoveThrottle = new Vector3(Input.GetAxis("Horizontal") * thisObject.Acceleration, counter, Input.GetAxis("Vertical") * thisObject.Acceleration);
    else
    thisObject.MoveThrottle = new Vector3(Input.GetAxis("Horizontal") * thisObject.Acceleration, 0f, Input.GetAxis("Vertical") * thisObject.Acceleration);
    }
    }
    else if (hit.collider.CompareTag("Ground") && thisObject.Controller.isGrounded)
    {
    counter = 0.6f;
    isGrounded = true;
    thisObject.MoveThrottle = new Vector3(Input.GetAxis("Horizontal") * thisObject.Acceleration, 0f, Input.GetAxis("Vertical") * thisObject.Acceleration);
    }
    else if (hit.collider.CompareTag("Cube"))
    {
    isGrounded = false;
    thisObject.MoveThrottle = new Vector3(Input.GetAxis("Horizontal") * thisObject.Acceleration, thisObject.JumpForce, Input.GetAxis("Vertical") * thisObject.Acceleration);
    }
    }
    }
    }


    I hope this sorts your problem with the new SDK (it does for me, though i don't have any use of this script for now anyway :P)

    Cheers, and keep on developing ;)

    Edit: You need to assign the playerBounce.cs script to the OVRPlayerController gameObject in order to make the script work properly ;) i somehow forgot to mention this before :P and do not change the values of the Counter or the isGrounded boolean as they work as expected ;)
  • Anonymous's avatar
    Anonymous
    Thanks again !

    I did all what you tell but i've got an error with playerbounce script :

    UnityException: Tag: Ground is not defined!
    playerBounce.Update () (at Assets/playerBounce.cs:21)

    Don't worry, you help me well, i know exactly where it come from and i'll try to find by myself beause i don't understand anything in C :?

    Thanks again for your help.
  • @thebko - it is quite easy and i don't mind :) but the error you get is because you haven't created a tag using the tag editor called "Ground" if you have another tag you want to use for grounds use can just rename "Ground" to whatever you want defined :) but always remember the " " it is very important or else you'll also get an error!

    Please do not hesitate to pm me if you have any further issues ;)

    Cheers.
  • Anonymous's avatar
    Anonymous
    Thanks to you, i'll try after work.

    Just a curiosity question : you seem to be an expert in unity, are you a pro programmer ?

    Bye !
  • "thebko" wrote:
    Thanks to you, i'll try after work.

    Just a curiosity question : you seem to be an expert in unity, are you a pro programmer ?

    Bye !


    Nope, i am by no means an expert... self-taught, yes!
    The funny thing here is that i actually started to learn JavaScript, but i always had an eager will to learn C# so i started trying to convert every script that i had in JavaScript :P i have also done so in the past but the best advice i can give you is to keep on developing, sooner or later you'll realize that you have come a LOOONG way since you started ;) so one day you might think that JavaScript seems a bit meh, but if that should happen, then please by all might, try and get to learn C# it'll definitely be worth it ;) but as you told me, you have only a couple of months of programming experience (at least, that was what i understood!) and that means you still have a lot to learn :) but never give up, there's millions of tutorials, both for JavaScript and C#, that's how i learned ;)

    But thanks for the compliment :mrgreen:
    Cheers.
  • braun0's avatar
    braun0
    Honored Guest
    "standtall007" wrote:
    Hi thebko, alright so i have remade the script though now it is in C# (hope that doesn't matter :P) so first of all you need to change the following script-lines in OVRPlayerController.cs :

    change
    protected CharacterController 	Controller 		 = null;


    to
    public CharacterController 	Controller 		 = null;


    and if the MoveThrottle variable line is not public then change it to:

    public Vector3 MoveThrottle   = Vector3.zero;


    then you need to create a script that is called playerBounce.cs (note it must be a C# script or else you'll get a lot of errors :( ) and paste these lines of code into the playerBounce.cs (you need to overwrite everything that is in the newly created script!)

    using UnityEngine;
    using System.Collections;

    public class playerBounce : MonoBehaviour
    {
    private bool isGrounded;
    private float counter = 0f;
    private OVRPlayerController thisObject;

    void Awake ()
    {
    thisObject = GetComponent<OVRPlayerController>();
    }

    void Update ()
    {
    RaycastHit hit;

    if (Physics.Raycast(transform.position, -Vector3.up, out hit, 1000f))
    {
    if (hit.collider.CompareTag("Ground") && !thisObject.Controller.isGrounded)
    {
    isGrounded = false;
    counter -= Time.deltaTime;
    if (!isGrounded)
    {
    if (counter > 0)
    thisObject.MoveThrottle = new Vector3(Input.GetAxis("Horizontal") * thisObject.Acceleration, counter, Input.GetAxis("Vertical") * thisObject.Acceleration);
    else
    thisObject.MoveThrottle = new Vector3(Input.GetAxis("Horizontal") * thisObject.Acceleration, 0f, Input.GetAxis("Vertical") * thisObject.Acceleration);
    }
    }
    else if (hit.collider.CompareTag("Ground") && thisObject.Controller.isGrounded)
    {
    counter = 0.6f;
    isGrounded = true;
    thisObject.MoveThrottle = new Vector3(Input.GetAxis("Horizontal") * thisObject.Acceleration, 0f, Input.GetAxis("Vertical") * thisObject.Acceleration);
    }
    else if (hit.collider.CompareTag("Cube"))
    {
    isGrounded = false;
    thisObject.MoveThrottle = new Vector3(Input.GetAxis("Horizontal") * thisObject.Acceleration, thisObject.JumpForce, Input.GetAxis("Vertical") * thisObject.Acceleration);
    }
    }
    }
    }


    I hope this sorts your problem with the new SDK (it does for me, though i don't have any use of this script for now anyway :P)

    Cheers, and keep on developing ;)

    Edit: You need to assign the playerBounce.cs script to the OVRPlayerController gameObject in order to make the script work properly ;) i somehow forgot to mention this before :P and do not change the values of the Counter or the isGrounded boolean as they work as expected ;)



    Hi standtall007: I was looking for a solution like this and came across this post. I have implemented the code as instructed, however I am only able to maintain the "bounce" physics/movement when the player is directly over the designated bounce "cube". If I am mid-bounce, at some height in the air, and I move the player over some other game object (not touching that other game object, just in the air above it), the player will immediately return to the ground and loses all horizontal movement.

    For example, I created a large "plane" game object and assigned it the "cube" tag so the script would identify it correctly (as per your instructions). I can bounce around on the plane all day long without issue. I can control the player while in the air, moving in any direction. However, if I insert a game object that I want the player to bounce over, the player loses all height & movement the instant the player enters the "air space" above the game object.

    I have been playing around with all the settings (Box Collider, tags, etc) but to no avail. I am hoping you might be able to provide some insight.

    Thank you.