How to integrate Jump into this javascript?

I’m using a script that came with a character from the asset store Since all his animations and everything is already programmed in javascript. If i can see it right, he just plays the animation while some kind of collider or whatever is attached to him stays in same position but the body moves up a bit. Anyway, the script. - Side notice, i’m a total noob when it comes to scripting

#pragma strict
internal var animator:Animator;
var v:float;
var h:float;
var run:float;
var jumpHeight = 8; //MY ATTEMPT

private var isFalling = false;
function Start () {
    animator=GetComponent (Animator);
}

function Update () {

    v=Input.GetAxis("Vertical");
    h=Input.GetAxis("Horizontal");
    if (animator.GetFloat("Run")==0.2){
        if (Input.GetKeyDown(KeyCode.Space)){
        GetComponent.<Rigidbody>().velocity.y = jumpHeight; // MY ATTEMPT
            animator.SetBool("Jump",true);
        }
    }
    Sprinting();
   
}

function FixedUpdate (){
    animator.SetFloat("Walk",v);
    animator.SetFloat("Run",run);
    animator.SetFloat("Turn",h);
}

function Sprinting(){
    if (Input.GetKey(KeyCode.LeftShift)){
        run=0.2;
    }
    else
    {
        run=0.0;
    }
}

Add a jump bool that you use in FixedUpdate to handle jumps:

var jump : boolean;

When the Space key is pressed, set this to true:

if(Input.GetKeyDown(KeyCode.Space))
{
    jump = true;
}

Then in fixed update, handle the physics stuff:

function FixedUpdate()
{
    if(jump)
    {  
        jump = false;
        GetComponent.<Rigidbody>().velocity  = Vector3(GetComponent.<Rigidbody>().velocity.x, jumpHeight, GetComponent.<Rigidbody>().velocity.z);
    }
}

You should store a reference to the rigidbody rather than using GetComponent every time you jump.

I don’t use JS so forgive me if you get any syntax errors :slight_smile:

Like this? Or did i type something at the wrong place :smile:. Thx for the help btw. This below ain’t working tho :P.

#pragma strict
internal var animator:Animator;
var v:float;
var h:float;
var run:float;
var jumpHeight = 8; //MY ATTEMPT
var jump : boolean;


function Start () {
    animator=GetComponent (Animator);
}

function Update () {

    v=Input.GetAxis("Vertical");
    h=Input.GetAxis("Horizontal");
    if (animator.GetFloat("Run")==0.2){
        if (Input.GetKeyDown(KeyCode.Space))
        {
         jump = true;

         animator.SetBool("Jump",true);
        }
    }
    Sprinting();
   
}

function FixedUpdate (){
    {
    animator.SetFloat("Walk",v);
    animator.SetFloat("Run",run);
    animator.SetFloat("Turn",h);
    }

    if(jump)
{
jump = false;
GetComponent.<Rigidbody>().velocity = Vector3(GetComponent.<Rigidbody>().velocity.x, jumpHeight, GetComponent.<Rigidbody>().velocity.z);
}

}


function Sprinting(){
    if (Input.GetKey(KeyCode.LeftShift)){
        run=0.2;
    }
    else
    {
        run=0.0;
    }
}

The jump code needs to be within the curly brackets in FixedUpdate:

function FixedUpdate()
{
    animator.SetFloat("Walk", v);
    animator.SetFloat("Run", run);
    animator.SetFloat("Turn",h);
  
    if(jump)
    {
        jump = false;
        GetComponent.<Rigidbody>().velocity = Vector3(GetComponent.<Rigidbody>().velocity.x, jumpHeight, GetComponent.<Rigidbody>().velocity.z);
    }
}

I would suggest learning and using C# instead of JS.