Anyone have any ideas on how I could improve my character jump to make it more realistic?
I would like the jump to last 0.7s from start to finish and beginning and ending of jump is slightly faster that the middle.
using System.Collections; using UnityEngine;
public class MoveOrb : MonoBehaviour {
public KeyCode moveL;
public KeyCode moveR;
public KeyCode jump;
public float slideSpeed = 0;
public float playerSpeed = 4;
public int laneNum = 2;
public string controlLocked = "n";
public float jumpHeight = 8;
public bool isAirBorn = false;
public float vertVel = 0;
void Start () {
}
void Update () {
//Move Character X Y Z
GetComponent<Rigidbody>().velocity = new Vector3(slideSpeed, vertVel, playerSpeed);
if((Input.GetKeyDown(moveL)) && (laneNum > 1) && (controlLocked == "n")) {
slideSpeed = -4;
StartCoroutine(stopSlide());
laneNum -= 1;
controlLocked = "y";
}
if((Input.GetKeyDown(moveR)) && (laneNum < 3) && (controlLocked == "n")) {
slideSpeed = 4;
StartCoroutine(stopSlide());
laneNum += 1;
controlLocked = "y";
}
if((Input.GetKeyDown(jump)) && (isAirBorn == false)) {
vertVel += 10;
isAirBorn = true;
StartCoroutine(stopJump());
}
}
void OnCollisionEnter(Collision other) {
if(other.gameObject.tag == "lethal") {
Debug.Log("Object Has Hit Lethal Block");
Destroy(gameObject);
}
if(other.gameObject.tag == "ground") {
isAirBorn = false;
}
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.name == "rampbottomTrig") {
vertVel = 1;
}
if (other.gameObject.name == "ramptopTrig") {
vertVel = 0;
}
}
IEnumerator stopSlide() {
yield return new WaitForSeconds(.3f);
slideSpeed = 0;
controlLocked = "n";
}
IEnumerator stopJump() {
yield return new WaitForSeconds(.35f);
vertVel = 0;
}
}
Thank you!
Answer by Spidlee · Mar 19, 2017 at 02:24 AM
Have you tried using RigidBody2d.Addforce() ? That should give you the desired momentum based jump with applied gravity.
https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html
Your answer
Follow this Question
Related Questions
How can I make it so that the longer you hold down the jump button the higher the player jumps? 0 Answers
Issues Getting my GameObject to both Move Forward and Jump properly. 0 Answers
Check Ground Detection not working. 0 Answers
Fast start and ending at jump but slow middle. 0 Answers
Can't jump when moving backwards and to the right or left (C#) 1 Answer