Rigidbody falling not according to physics, only when script is applied.
Hi,
I've read about this topic for the past 3 hours in an attempt to fix it without asking for help, but I just can't find a solution.
The physics in my scene work perfectly for the primitives I have in there, except for my player. The second I add the playerController script to any asset with a Rigidbody on it they stop being affected by physics the same way.
I've tried upping the gravity, changing the scale of the object, and increasing forces artificially (though my current novice skills fail me). Yet here I am still dumbfounded.
If I import a cube, it falls perfectly fine. I attach my script (basic left, right, sprint and jump functions) and it suddenly acts really light and floaty.
Can anybody help me please?
{
public float walkSpeed;
public float runSpeed;
//JUMP VARS
bool playerGrounded = false; //Is the player on the ground?
Collider[] groundCollisions; //??
float groundCheckRadius = 0.2f; //
public LayerMask groundLayer; //Set in Unity, Layer for ground
public Transform groundCheck; //Set in Unity, OBJECT for ground detection
public float jumpHeight; //jump height
bool running;
Rigidbody playerRB;
// Use this for initialization
void Start()
{
playerRB = GetComponent<Rigidbody>();
}
void FixedUpdate () {
//Movement left and right
running = false;
float move = Input.GetAxis("Horizontal"); //Assigns A and D to move +1 or -1
float sprint = Input.GetAxis("Fire3"); //Shift
if ((move > 0 || move < 0) && (sprint > 0 && playerGrounded)) //If A or D && Shift && Grounded
{
playerRB.velocity = new Vector3(move * runSpeed, 0, 0); //This one triggers if moving and holding shift whilst moving
}
else
{
playerRB.velocity = new Vector3(move * walkSpeed, 0, 0); //This one allows movement
}
//----
//JUMPING
//Set the ground check collider
groundCollisions = Physics.OverlapSphere(groundCheck.position, groundCheckRadius, groundLayer);
//Is the player on the ground?
if (groundCollisions.Length > 0) //Array.Length - A collision will add 1 or more to the array
{
playerGrounded = true;
}
else
{
playerGrounded = false;
}
if (playerGrounded && Input.GetAxis("Jump") > 0)
{
playerGrounded = false;
playerRB.velocity = new Vector3(playerRB.velocity.x, 0, 0); //Sets the Y velocity (up/down) to 0 for consistency
playerRB.AddForce(0, jumpHeight, 0, ForceMode.Impulse);
}
} //end of fixed update
}
Any help you can provide will be greatly appreciated, thanks for your time, and I apologise if this question has already been answered, it's now 3am and I've been sat here trying not to pull my own eyes out since midnight. Thanks again.
playerRB.velocity = new Vector3(move * runSpeed, 0, 0);
If you set the velocity directly, you're overriding any velocity that might be caused by other forces, e.g. gravity. Use AddForce() ins$$anonymous$$d, which will then be added to all other forces.
That worked, thanks! However I have used this before and it works..
{ if ((sneaking > 0 || firing > 0) && playerGrounded)
{
playerRb.velocity = new Vector3(move walkSpeed, playerRb.velocity.y, 0);
} else
{ playerRb.velocity = new Vector3(move runSpeed, playerRb.velocity.y, 0);
if ($$anonymous$$athf.Abs(move) > 0) running = true; } }
and this is affected by gravity fine.
Your answer
Follow this Question
Related Questions
Rigidbody can push but can't be pushed 2 Answers
Player moves indefinitely after collision 0 Answers
Player Movement Goes Crazy When Hit On Side Of Moving Platform 0 Answers
Ball Speed is not increasing as per code 0 Answers
Making a rigidbody "immune" to reacting with another specific rigidbody? 0 Answers