Sunday 15 October 2017

The Best Way to jump in Unity3d

Attach these two scripts with your player  and feel the difference in jumping it works both for 3D and 2D for 2D you have to make some adjustments in betterJump script.

WATCH VIDEO TUTORIAL

Player Script:


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

public class Player : MonoBehaviour {
    public float speed = 10.0F;
    public float rotationSpeed = 100.0F;
    // Use this for initialization
    void Start () {
  
 }
 
 // Update is called once per frame
 void Update () {
 
        float translation = Input.GetAxis("Vertical"* speed;
        float rotation = Input.GetAxis("Horizontal"* rotationSpeed;
 
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
 
        transform.Translate(00, translation);
        transform.Rotate(0, rotation, 0);
 
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jump();
        }
    }
    void jump()
    {
        transform.GetComponent<Rigidbody>().AddForce(new Vector3(0190 * Time.deltaTime, 0),ForceMode.Impulse);
    }
}



Better Jump Script (for 3D project)

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

public class fastJump : MonoBehaviour {
 
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;
    Rigidbody rb;
 
 
    void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        if (rb.velocity.< 0)
        {
            rb.velocity += Vector3.up * Physics.gravity.* (fallMultiplier - 1)
 * Time.deltaTime;
        }
    }
}

Better Jump Script (for 2D project)

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

public class fastJump : MonoBehaviour {
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;
    Rigidbody2D rb;
    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        if (rb.velocity.< 0)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.*
                (fallMultiplier - 1* Time.deltaTime;
        }
    }
 
 
}

No comments:

Post a Comment