- Home /
Rigidbody movement conflict?
Hello guys! I've got a problem. When I hold AWSD then hold spacebar, it just ends up with the character getting up a little from the ground then going back to normal, like as if nothing happened! Is there some way to fix this? Here is my script:
function OnTriggerStay ()
{
Jumped = false;
}
function FixedUpdate ()
{
//Player movement
if(Input.GetKey(KeyCode.Space) && !Jumped)
{
Jumped = true;
rigidbody.AddForce(Vector3.up * JumpHeight);
}
if(Input.GetKey(KeyCode.W))
rigidbody.velocity = transform.forward * Forward * Time.deltaTime;
if(Input.GetKey(KeyCode.S))
rigidbody.velocity = transform.forward * -MoveSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.D))
rigidbody.velocity = transform.right * MoveSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.A))
rigidbody.velocity = transform.right * -MoveSpeed * Time.deltaTime;
//Player run movement
if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
if(Input.GetKey(KeyCode.W))
rigidbody.velocity = transform.forward * ForwardRun * Time.deltaTime;
if(Input.GetKey(KeyCode.S))
rigidbody.velocity = transform.forward * -RunSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.D))
rigidbody.velocity = transform.right * RunSpeed * Time.deltaTime;
if(Input.GetKey(KeyCode.A))
rigidbody.velocity = transform.right * -RunSpeed * Time.deltaTime;
}
}
I'm guessing it's because I'm using rigidbody movement for both commands? Sorry, only started Unity about a week ago.
you might need to check the strength of your gravity level to see how high it is.
I didn't change the gravity, just activated it on my rigidbody component
Answer by AlucardJay · Apr 08, 2014 at 06:04 AM
It is because immediately after adding force you are hard setting the velocity of the rigidbody, overriding the force added. You need to maintain the Y-velocity.
Edit : Sorry for the late reply. I didn't have time to give an example, but tbkn has given an example of what I was describing. And it seems like my comment was converted to answer, so I have stripped down one of my player code projects to give an example for future readers. Even though stripped down it is quite long, the main part is in the function ApplyMovement() where (again as tbkn demonstrated) the rigidbody velocity Y is maintained in the calculated velocity before applying to the rigidbody.
calcVel.y += myRigidbody.velocity.y;
Hopefully you'll see some interesting stuff in my below code :
//------------------------------//
// RigidbodyMovement.js //
// Written by Alucard Jay //
// 4/8/2014 //
//------------------------------//
#pragma strict
public var moveSpeed : float = 7.0;
public var jumpVelocity : float = 120.0;
public var airMovementModifier : float = 0.1;
public var velocityLerpTime : float = 0.05;
public var maxSlopeAngle : float = 60.0;
public var inputDeadZone : float = 0.05;
public var isAlive : boolean = false;
private var myTransform : Transform;
private var myRigidbody : Rigidbody;
private var desiredVelocity : Vector3 = Vector3.zero;
private var inputVector : Vector3 = Vector3.zero;
private var isGrounded : boolean = false;
private var isJumping : boolean = false;
// Persistant Functions
// ------------------------------------------------------------------------------------------------
function Awake()
{
Initialize();
}
function Start()
{
myTransform = transform;
myRigidbody = transform.rigidbody;
}
function Update()
{
if ( !isAlive )
{
return;
}
// -- MOVEMENT --
GetPlayerInput();
ApplyMovement();
}
function FixedUpdate()
{
if ( isAlive )
{
myRigidbody.velocity = desiredVelocity;
}
}
// Other Functions
// ------------------------------------------------------------------------------------------------
function Initialize()
{
isAlive = true;
}
// PLAYER INPUT
// ------------------------------------------------------------------------------------------------
function GetPlayerInput()
{
// reset inputVector
inputVector = Vector3.zero;
// get movement inputs
var horz : float = Input.GetAxis("Horizontal");
var vert : float = Input.GetAxis("Vertical");
// get player Vertical input
if ( vert < -inputDeadZone || vert > inputDeadZone )
{
inputVector += Vector3( 0, 0, vert );
}
// get player Horizontal input
if ( horz < -inputDeadZone || horz > inputDeadZone )
{
inputVector += Vector3( horz, 0, 0 );
}
// get player Jump input
if ( Input.GetKeyDown(KeyCode.Space) )
{
isJumping = true;
}
}
// APPLY MOVEMENT
// ------------------------------------------------------------------------------------------------
function ApplyMovement()
{
// calculate the direction for the force to be applied
var calcVel : Vector3 = myTransform.TransformDirection( inputVector.normalized ) * moveSpeed;
// check if jumping, add Jump velocity,
// else maintain current Y velocity
if ( isJumping )
{
calcVel.y = jumpVelocity;
isJumping = false;
}
else
{
// maintain current Y velocity
//calcVel.y = myRigidbody.velocity.y;
// add velocity.y instead of hard setting it
calcVel.y += myRigidbody.velocity.y;
}
// calculate desired velocity
if ( !isGrounded )
{
// calculate desired velocity
calcVel *= airMovementModifier; // restrict movement while airborne
calcVel += myRigidbody.velocity * ( 1.0 - airMovementModifier ); // maintain velocity while in the air
desiredVelocity = Vector3.Lerp( myRigidbody.velocity, calcVel, velocityLerpTime );
}
else
{
// calculate desired velocity
desiredVelocity = Vector3.Lerp( myRigidbody.velocity, calcVel, velocityLerpTime );
}
}
// Is Grounded Collision Events
// ------------------------------------------------------------------------------------------------
var currSlopeAngle : float = 0.0; // Debugging slope angle
function OnCollisionEnter( collision : Collision )
{
for ( var contact : ContactPoint in collision.contacts )
{
if ( Vector3.Angle( contact.normal, Vector3.up ) < maxSlopeAngle )
{
isGrounded = true;
}
// ----
currSlopeAngle = Vector3.Angle( contact.normal, Vector3.up );
// ----
}
}
function OnCollisionStay( collision : Collision )
{
for ( var contact : ContactPoint in collision.contacts )
{
if ( Vector3.Angle( contact.normal, Vector3.up ) < maxSlopeAngle )
{
isGrounded = true;
}
// ----
// Debugging slope angle
// character is climbing slopes
currSlopeAngle = Vector3.Angle( contact.normal, Vector3.up );
// ----
}
}
function OnCollisionExit()
{
isGrounded = false;
}
Try the following commands to keep the original y-velocity when manually setting the rigidbody velocity:
Vector3 tempVelocity;
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
tempVelocity = transform.forward * Forward * Time.deltaTime; // change this in all you ifs, ins$$anonymous$$d of rigidbody.velocity set tempVelocity
...
// now set the velocity, using the original y
tempVelocity.y = rigidbody.velocity.y;
rigidbody.velocity = tempVelocity;
It works! It also solves my problem about character falling down slowly. Thanks!
I updated the comment (which somehow became an answer!) with a stripped down version of one of my controllers. @tbkn has given a great example of what I was trying to say.
Your answer
Follow this Question
Related Questions
How to move Character with AddForce 1 Answer
Question regarding Ridigbody2D.velocity.magnitude 1 Answer
Ball moving in a tube 0 Answers
Why is velocity checking intensive? 0 Answers
rigidbody velocity on x axis and addforce on y axis ?? 1 Answer