Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 ThePhantomPsychic · Jul 21, 2015 at 09:10 AM · c#scripting problemjump

I need help with a jump.

I am coding a jump, and having a heck of a time trying to figure out how to make it work. It's a sprite with a 3d rigidbody, because I am making a fan paper mario game. I have been trying to get him off the ground, which I have successfully done, but it's a bit choppy. What I haven't been able to do is get him up to about 2 float above the ground, and use gravity to bring him back down.

Here is my coding for all of his movement:

     using UnityEngine;
 using System.Collections;
 
 public class MarioMovement : MonoBehaviour {
     public float speed = -6f;            // The speed that the player will move at.
     
     Vector3 movement;                   // The vector to store the direction of the player's movement.
     Animator anim;                      // Reference to the animator component.
     Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
     public float up = 0f;
 
     void Awake ()
     {
         // Set up references.
         anim = GetComponent <Animator> ();
         playerRigidbody = GetComponent <Rigidbody> ();
         playerRigidbody.useGravity = true;
     }
 
     void Update ()
     {
         // Store the input axes.
         float h = Input.GetAxisRaw ("Horizontal");
         float v = Input.GetAxisRaw ("Vertical");
         bool u = Input.GetKey (KeyCode.Space);
 
         // Move the player around the scene.
         Move (h, v, up);
         
         // Animate the player.
         Animating (h, v, up);
 
         Jump (h, v, up, u);
     }
     
     void Move (float h, float v, float up)
     {
         // Set the movement vector based on the axis input.
         movement.Set (h, up, v);
         
         // Normalise the movement vector and make it proportional to the speed per second.
         movement = movement.normalized * speed * Time.deltaTime;
         
         // Move the player to it's current position plus the movement.
         playerRigidbody.MovePosition (transform.position + movement);
     }
     
     void Animating (float h, float v, float up)
     {
         // Create a boolean that is true if either of the input axes is non-zero.
         bool Running = h != 0f || v != 0f;
         
         // Tell the animator whether or not the player is walking.
         anim.SetBool ("Is Running", Running);
         anim.SetFloat ("v", v);
 
         if (Running = v == 6f)
             anim.SetBool ("Is Facing Up", true);
         else
             anim.SetBool ("Is Facing Up", false);
 
 
     }
 
     void Jump (float h, float v, float up, bool u)
     {
         if (u == true) {
             up = -7f;
             if (transform.position.y >= 1f)
                 up = 7f;
         }
 
         if (u == false && transform.position.y >= 1f)
             up = 7f;
 
         if (up != 0f)
             anim.SetBool ("Is Jumping", true);
         else
             anim.SetBool ("Is Jumping", false);
 
         movement.Set (h, up, v);
         
         // Normalise the movement vector and make it proportional to the speed per second.
         movement = movement.normalized * speed * Time.deltaTime;
         
         // Move the player to it's current position plus the movement.
         playerRigidbody.MovePosition (transform.position + movement);
     }
 }    

 

It is all in c#, as you now can see, so if anyone can help, please respond with answers in c#. Thank you.

Comment
Add comment · Show 1
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 iamsidv · Jul 21, 2015 at 10:13 AM 0
Share

if it is a rigidbody, then you should use rigidBody.AddForce(Vector3.up, Force$$anonymous$$ode.Impulse);

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ReCoF · Jul 21, 2015 at 11:42 AM

Hi,

You should keep a look at RigidBody.AddForce()

http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

then be sure that your player if affected by the gravity and you just have to find to correct value for your jump in order to be as perfect as you wish.

Comment
Add comment · Show 1 · 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 ThePhantomPsychic · Jul 21, 2015 at 01:48 PM 0
Share

Yes, it is affected by gravity. Only problem is that he is a sprite. Should I have given him a 2d rigidbody rather than a 3d rigidbody? Because the gravity does not affect him at all. If you look at the script, I am using Vector 3 for jumping. Should I be using that?

And yes, I did look at that before, but I do not know how to use it. I know unity can give you examples on how to use it, but sometimes they are not very helpful.

avatar image
0

Answer by spalmer · Jul 21, 2015 at 03:39 PM

The main problem you had originally is you were trying to micromanage the jump, and since you call MovePosition each frame, overrides any gravity the physics engine applies. Using AddForce is correct. If you want to calculate how much force to add to get your character to achieve a certain height off the ground at the apex of the jump, you can do that using elementary physics given the character's mass m and the desired height h and gravity force Vector3 g = Physics.gravity, something like: rigidbody.AddForce(-Mathf.Sqrt(2.01f * h * g.magnitude) * g.normalized, ForceMode.VelocityChange); I believe the nintendo character has nonstandard gravity physics; he doesn't obey standard Newtonian rules, has some extra lift force involved with how you're pushing the button which makes him float slightly.

One more thing: You must set the character's rigidbody.useGravity = true and rigidbody.isKinematic = false

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 ThePhantomPsychic · Jul 21, 2015 at 05:09 PM 0
Share

If it isn't too much to ask, could you use my script to show how to put your way in? I have seen many problems like this, but I don't know where to put it.

Also, do you know what that other nonstandard code is? (just out of curiosity...)

avatar image spalmer · Jul 22, 2015 at 01:42 AM 0
Share

Sorry I'm not privy to details about their movement.

In your Awake(), just add playerRigidbody.is$$anonymous$$inematic = false; I don't understand what your old Jump method is doing. Stuff like testing if v == 6 mixed with an assignment is poor style, modifying your input parameters is just confusing. I wish I had time to just write you a class.

avatar image ThePhantomPsychic · Jul 22, 2015 at 08:57 PM 0
Share

Well, actually, I don't $$anonymous$$d...

In that case, could you try a completely new script ins$$anonymous$$d of $$anonymous$$e?

avatar image
0

Answer by jonathanhuo11 · Jul 23, 2015 at 01:28 AM

Hi,

I've been using Unity for its 2D mode for a while. If you want a proper jump, then there are a few things to consider:

  1. Ground Checking - is my character on the ground? If so, allow him to jump. If not, do not allow him to jump.

  2. Jump mechanism - how will I make my character jump? There are many ways you can make a character jump.

So after considering these things, I think the best option for you would be to use layermasking to control your ground collision test. The best way to do so would be to use Physics.OverlapSphere(Vector3 point, float radius, LayerMask layermask) to test if a sphere, with a center point located at point (point is presumably the location of the character's feet), and radius of radius, overlaps any objects with the layermask of ground. If so, then allow player to jump, because they are on the ground. To provide thrust for the jump, use Rigidbody.ApplyForce(Vector3 force), and apply the necessary force to lift the player off the ground.

If my wording was confusing, or you didn't get some of the concepts, watch the 2D character controller video. Although it is referring to 2D sprites, the basic concept is the same, and all you need to do is convert to 3D rigidbodies and such.

Jon

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
avatar image
0

Answer by ThePhantomPsychic · Jan 27, 2016 at 03:05 PM

Alright, I figured this out on my own, sort of. For starters, is kinematic was on, and second up, my drag and angled drag values were set to infinity.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Jumping in 3D 1 Answer

Jumping in only the direction of the last key pressed 0 Answers

How to check if character reached jump apex / peak 1 Answer


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