Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JoshuaOreskovich · May 07, 2019 at 02:55 AM · jumptranslate

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);
         }
     }   

 }


}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoshuaOreskovich · May 07, 2019 at 01:16 PM 0
Share

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!).

avatar image K-Anator JoshuaOreskovich · May 07, 2019 at 04:46 PM 1
Share

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*

avatar image K-Anator K-Anator · May 07, 2019 at 04:50 PM 0
Share

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);

Show more comments
avatar image
1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoshuaOreskovich · May 07, 2019 at 01:07 PM 1
Share

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!

avatar image Bunny83 JoshuaOreskovich · May 07, 2019 at 01:20 PM 1
Share

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.

avatar image JoshuaOreskovich Bunny83 · May 07, 2019 at 02:12 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges