simple jump script unity 3d code example

Example 1: unity how to make jump script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class jump : MonoBehaviour
{
    public float jumpHeight = 7f;
    public bool isGrounded;
    public float NumberJumps = 0f;
    public float MaxJumps = 2;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        if (NumberJumps > MaxJumps - 1)
        {
            isGrounded = false;
        }

        if (isGrounded)
        {
            if (Input.GetButtonDown("Jump"))
            {
                rb.AddForce(Vector3.up * jumpHeight);
                NumberJumps += 1;
            }
        }
    }

    void OnCollisionEnter(Collision other)
    {
        isGrounded = true;
        NumberJumps = 0;
    }
    void OnCollisionExit(Collision other)
        {
            
    }
}

Example 2: slomotion in unity 3d

using UnityEngine;
using System.Collections;

public class SlowMotion : MonoBehaviour {

float currentAmount = 0f;
float maxAmount = 5f;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if(Input.GetKeyDown ("insert any key here")){

if(Time.timeScale == 1.0f)
Time.timeScale = 0.3f;

else

Time.timeScale = 1.0f;
Time.fixedDeltaTime = 0.02f * Time.timeScale;
}


if(Time.timeScale == 0.03f){

currentAmount += Time.deltaTime;
}

if(currentAmount > maxAmount){

currentAmount = 0f;
Time.timeScale = 1.0f;

}

}
}

Tags:

Misc Example