Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 boddole · Jan 19, 2014 at 07:04 AM · c#physicsrigidbody

Unexpected "Jumping" Behavior

Hello everyone, I've been working with rigidbodies and physics, and I have made a basic character "jump" function (among other things), however, it is not performing in the way I thought it would.

While stationary, the character jumps higher and (if motion is applied after the jump) as far or farther than a character running at full speed. I could understand the height result (as it makes sense that as a person runs faster, their jump trajectory becomes flatter (don't know if that is really true)), but I would think that they should certainly jump further since they were already moving at max speed.

If you have any ideas why this is happening, I'd love to know.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     public float playerForwardSpeed;
     public float playerAcceleration;
     public float playerDecceleration;
     public float playerMaxForwardSpeed;
     public float playerMaxBackwardSpeed;
     public float playerCurrentSpeed;
 
     public Vector3 eulerAngleVelocity;
     public Vector3 playerJumpHeight;
 
 
 
     void FixedUpdate ()
     {
         MovePlayer ();
         playerCurrentSpeed = Mathf.Abs(rigidbody.velocity.z);
 
 
     }
     
     void MovePlayer ()
     {
         Quaternion positiveRotation = Quaternion.Euler(eulerAngleVelocity * Time.fixedDeltaTime);
         Quaternion negativeRotation = Quaternion.Euler(-eulerAngleVelocity * Time.fixedDeltaTime);
 
         //setting forwards and backwards
         if (Input.GetAxisRaw ("Vertical") == 1)
             {
             rigidbody.velocity += transform.forward * playerForwardSpeed * playerAcceleration * Time.fixedDeltaTime;
             rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, Mathf.Abs(playerMaxForwardSpeed));
             }
 
         if (Input.GetAxisRaw ("Vertical") == -1)
         {
             rigidbody.velocity += (transform.forward * -playerForwardSpeed) * Time.fixedDeltaTime;
             rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, Mathf.Abs(playerMaxBackwardSpeed));
         }
 
         //setting rotation
         if (Input.GetAxisRaw ("Horizontal") == 1)
             {
             rigidbody.MoveRotation(rigidbody.rotation * positiveRotation);
             }
 
         if (Input.GetAxisRaw ("Horizontal") == -1)
         {
             rigidbody.MoveRotation(rigidbody.rotation * negativeRotation);
         }
 
         //setting jump
         if (Input.GetButton ("Jump"))
         {
             RaycastHit hit;
             Physics.Raycast (transform.position, -Vector3.up ,out hit, 1.0f);
             if (hit.collider != null)    
             {
                 rigidbody.AddForce (playerJumpHeight,ForceMode.Impulse);
             }
         }
     }
 }
Comment
Add comment · Show 2
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 robertbu · Jan 19, 2014 at 08:25 AM 0
Share

It is hard to verify this code without seeing the initialized values ...in particular eulerAngleVelocity and playerJumpHeight. Put a debug.Log() in to verify that the 'Y' value of transform.forward is always 0.

avatar image boddole · Jan 19, 2014 at 10:36 AM 0
Share

Threw in debug.log statements, Y is always 0. Added the numbers to the code, hopefully it helps.

 using UnityEngine;
 using System.Collections;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
     public float playerForwardSpeed = 10f;
     public float playerAcceleration = 1.1f;
     public float playerDecceleration = 1.05f;
     public float player$$anonymous$$axForwardSpeed = 7f;
     public float player$$anonymous$$axBackwardSpeed = 4.5f;
     public float playerCurrentSpeed;
 
     public Vector3 eulerAngleVelocity = new Vector3 (0f,100f,0f);
     public Vector3 playerJumpHeight = new Vector3 (0f,5f,0f);
 
 
 
     void FixedUpdate ()
     {
         $$anonymous$$ovePlayer ();
         playerCurrentSpeed = $$anonymous$$athf.Abs(rigidbody.velocity.z);
 
 
     }
     
     void $$anonymous$$ovePlayer ()
     {
         Quaternion positiveRotation = Quaternion.Euler(eulerAngleVelocity * Time.fixedDeltaTime);
         Quaternion negativeRotation = Quaternion.Euler(-eulerAngleVelocity * Time.fixedDeltaTime);
 
         //setting forwards and backwards
         if (Input.GetAxisRaw ("Vertical") == 1)
             {
             rigidbody.velocity += transform.forward * playerForwardSpeed * playerAcceleration * Time.fixedDeltaTime;
             rigidbody.velocity = Vector3.Clamp$$anonymous$$agnitude(rigidbody.velocity, $$anonymous$$athf.Abs(player$$anonymous$$axForwardSpeed));
             Debug.Log (transform.forward.y);
             }
 
         if (Input.GetAxisRaw ("Vertical") == -1)
         {
             rigidbody.velocity += (transform.forward * -playerForwardSpeed) * Time.fixedDeltaTime;
             rigidbody.velocity = Vector3.Clamp$$anonymous$$agnitude(rigidbody.velocity, $$anonymous$$athf.Abs(player$$anonymous$$axBackwardSpeed));
             Debug.Log (transform.forward.y);
         }
 
         //setting rotation
         if (Input.GetAxisRaw ("Horizontal") == 1)
             {
             rigidbody.$$anonymous$$oveRotation(rigidbody.rotation * positiveRotation);
             Debug.Log (transform.forward.y);
             }
 
         if (Input.GetAxisRaw ("Horizontal") == -1)
         {
             rigidbody.$$anonymous$$oveRotation(rigidbody.rotation * negativeRotation);
             Debug.Log (transform.forward.y);
         }
 
         //setting jump
         if (Input.GetButton ("Jump"))
         {
             RaycastHit hit;
             Physics.Raycast (transform.position, -Vector3.up ,out hit, 1.0f);
             if (hit.collider != null)    
             {
                 rigidbody.AddForce (playerJumpHeight,Force$$anonymous$$ode.Impulse);
                 Debug.Log (transform.forward.y);
             }
         }
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

18 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

Related Questions

RigidBodies and "Slipping" Prevention 0 Answers

RigidBody Script Conflict 1 Answer

Particle collision with a rigidbody in C# 1 Answer

Rigidbody character controller can't walk on stairs 0 Answers

Unity Script causing crash 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