- Home /
Trying to create a jump script without velocity
I've done literally weeks of homework on this issue with no luck. I need to create a jump without using Unity's rigidbody physics. I can make a jump with transform.translate well enough, but not one that looks and works well for character movement. Anyone have a solve to create a good jump mechanic without Using unity's physics engine?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class movement : MonoBehaviour {
[SerializeField] float throttle = 6f; // pawn speed
[SerializeField] Rigidbody rb;
[SerializeField] Vector3 pStop;
[SerializeField] Vector3 pStart;
[SerializeField] float rotspeed = 100f;
public bool isGrounded = true;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// **** W,Q,S,E *******************************************************************
if (Input.GetAxis("Vertical") > 0 || Input.GetAxis("Vertical") < 0)
{
transform.position += transform.forward * throttle * Time.deltaTime * Input.GetAxis("Vertical");
}
if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)
{
transform.position += transform.right * throttle * Time.deltaTime * Input.GetAxis("Horizontal");
}
// **** END W,Q,S,E ***************************************************************
// **** A,D ***********************************************************************
float turn = rotspeed * Time.deltaTime * Input.GetAxisRaw("Turn");
transform.Rotate(0, turn, 0);
// **** END A,D *******************************************************************
// **** IF GROUNDED ***************************************************************
//reset jump
if (Physics.Raycast(transform.position, new Vector3(0, -1, 0), 1.35f))
{
isGrounded = true;
}
//make sure jump isn't possible while no ground
else
{
isGrounded = false;
}
// **** END IF GROUNDED ***********************************************************
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
//i += Time.deltaTime;
//transform.position = Vector3.Lerp(pStart, pStop, i);
//transform.position = transform.up * Time.deltaTime;
pStart = transform.position;
pStop = transform.position + new Vector3(0, 5f, 0);
float t = 0f;
while (t < 1)
{
t += Time.deltaTime;
transform.position = Vector3.Lerp(pStart, pStop, t);
}
}
}
}
Answer by K-Anator · May 07, 2019 at 03:41 AM
https://youtu.be/7KiK0Aqtmzc might be of some use. He is using physics, but the concepts he goes over explains why games like Super Mario Bros. have those jumps that just feel right. Basically make the player fall faster than they should. Why the aversion to Unity's physics?
I appreciate your response, thank you for taking the time to do so! the reason I am avoiding the unity physics for jumping is because things like .Addforce and .velocity seem to create other problems. Like overly fickle jumping mechanics or floaty jumps where too much inaccuracy was creating one problem or another. I know there are different ways to skin the cat so i decided to try to simply create my own jump (again, sigh!).
Gotcha, though in the end, if you do things right, you should end up with a jump that looks and acts like you did actually use unity's physics. Like Board to Bits explains in his video, the reason a physically accurate jump doesn't feel right, is because we play video games. rb.addVelocity(vector3.up, jumpForce)
will produce a physically accurate jump. But that's no fun. I would suggest giving BtB's jump a try, it pretty closely emulates the jump in Super $$anonymous$$ario Bros., but with some tweaking you should be able to get whatever results you're after. For example the jumping in what I'm working on right now is just me adding velocity up player.velocity += new Vector3(0, JumpCharge, 0);
then I make the player fall faster because it was feeling too floaty.
if (player.velocity.y < 0)
{
if (groundDist.distance >= hoverHeight + 1 || !groundDist.collider) //Check if we're above the hover height, or not above anything
player.velocity += new Vector3(0, Physics.gravity.y * (Fall$$anonymous$$ultiplier - 1) * Time.deltaTime, 0); //fall faster
}
This results in a very predicable, controllable jump. $$anonymous$$y fall multiplier is around 4*
One thing to keep in $$anonymous$$d, is that you want to add velocity, not set it. And how you add the force can change things as well. player.AddForce(appliedHoverForce, Force$$anonymous$$ode.Acceleration);
will produce different results than player.AddForce(appliedHoverForce, Force$$anonymous$$ode.Impulse);
Answer by Bunny83 · May 07, 2019 at 03:41 AM
What you have inside your jump key if-body doesn't make much sense. You essentially need those things:
gravity
y velocity
Something like this:
float yVel = 0; // keep track of your current yVelocity
float gravity = 9.81; // gravity acceleration
void Update()
{
// [ ... ]
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
yVel = 5; // jump impulse force
isGrounded = false;
}
if (isGrounded)
{
yVel = 0;
}
else
{
yVel -= gravity * Time.deltaTime;
}
transform.position += transform.right * yVel * Time.deltaTime;
}
Note that since we now move up and down from the ground you probably should adjust the y position when you "land" (when you set isGrounded). The raycast will give you the distance to the ground so once your ray hits the ground you probably want to set your character to a certain distance from the ground so the ray still intersects. Otherwise you may "land" on slightly different y positions. This is just the "straight-forward" implementation.
Since gravity depends on the time squared it's actually not framerate independent. For more information see this question or this
Thank you kindly for your response! However, I wasn't clear enough with my original question and I am still in need of help. I am actually using rigidbody/gravity, but not built-in velocity. I am attempting to create a jump that includes those things, but doesn't include using rigidbody.velocity or Addforce etc. you answered the question to an extent, but didn't provide enough of a tangible solution. After rewriting the if/jump statement based around wheat you had given me I'm off to a better start, but I am still back where I started w/o enough information to finish.
[SerializeField] float yVel = 5;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && isGrounded == true) { transform.position += transform.up yVel Time.deltaTime; }
This code still 'jumps' but the jump is still instantaneous and not over time so that it will look and work functionaly.
Again thank you for you effort!
Sorry but what you said just makes no sense at all. A rigidbody is a simulated rigidbody object. It has a $$anonymous$$imum state information of it's current positionm, rotation, linear velocity and angular velocity. Whenever you move a rigidbody in Unity with transform.Translate or by directly modifying the position / rotation you actually bypass any actual physics calculations.
The build in gravity does pretty much what i did for my own velocity. Gravity is an acceleration which soley works on / influences the current velocity.
So now we're on a point where it's completely unclear what you actually want to do. You said you want to use a rigidbody physics simulation (where velocity is a part of an deter$$anonymous$$es how fast the object is moving in all 3 axis) on the other hand you said you don't want to use velocity. All your current movement code is already wrong if you actually want to use a rigidbody. Your movement code actually forces your object to penetrate into other colliders. At this point the rigidbody will actually apply penalty forces to solve this intersection. The forces applied by the physicsystem will directly change the velocity.
ok I think I see what you are saying about a lack of clarity.
I'm trying to 'jump up' without physics (slowly, not instantly) and fall with rigidbody gravity.
Answer by JoshuaOreskovich · May 09, 2019 at 12:51 AM
Although K-ANator's answer doesn't address my original problem, he solved my bigger problem. Which was more important. My original code was causing erratic jump behavior due to a misplaced '=' sign I believe. I am no longer experiencing the problem with my jump and I am very, very thankful.
Your answer
Follow this Question
Related Questions
Lerp jump using input won't convert to world space 1 Answer
Jump and Translate on the same time 0 Answers
Problems with jump script 1 Answer
transform.Translate and transform.eulerAngles bug 0 Answers
Diagonal Wall Jump 0 Answers