- Home /
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);
}
}
}
}
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.
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);
}
}
}
}
Your answer
Follow this Question
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