Rigidbody.velocity Movement code produce wildly different gravity interactions
Main character movement using Rigidbody.velocity and two methods of control for player preference. The character moves via WASD (either locally or globally) and rotates to face the direction of the mouse pointer. I can confirm that the aiming script does not manipulate the problem, it is just this script.
The problem I am facing is that the two methods interact with the player differently, as intended, however while 'Scheme2' correctly follows physics (namely gravity, force modifiers etc), 'Scheme1' has this effect where the character floats extremely slowly towards the ground after moving off a ledge.
No amount of applied force or the absense of applied forces seem to affect the rate of fall. As stated, 'Scheme2' interacts as intended with gravity.
Why does 'Scheme1' fall at an absurdly slow rate?
My code for the movement script is posted below:
public class PlayerMovement : MonoBehaviour
{
public float speed = 0.0f;
private Rigidbody PhysicsBody;
//Relevant to current control scheme testing
private bool Scheme1; //Default Control Scheme: Screen-Based
private bool Scheme2; //Alternate Control Scheme: Pointer-Based
void Start ()
{
PhysicsBody = GetComponent<Rigidbody>();
//At the start of the game set Control Scheme to Default
Scheme1 = true;
Scheme2 = false;
}
void FixedUpdate ()
{
if (Scheme1 == true)
{
var PlayerInput = new Vector3 (Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
PhysicsBody.velocity = (PlayerInput * speed * Time.deltaTime);
}
if (Scheme2 == true)
{
//forward
if (Input.GetAxis ("Vertical") > 0)
{
PhysicsBody.velocity = (transform.forward * speed * Time.deltaTime);
}
//back
if (Input.GetAxis ("Vertical") < 0)
{
PhysicsBody.velocity = (-transform.forward * speed * Time.deltaTime);
}
//right
if (Input.GetAxis ("Horizontal") > 0)
{
PhysicsBody.velocity = (transform.right * speed * Time.deltaTime);
}
//left
if (Input.GetAxis ("Horizontal") < 0)
{
PhysicsBody.velocity = (-transform.right * speed * Time.deltaTime);
}
}
}
void OnGUI ()
{
if(GUI.Button(new Rect(50,50, 200, 50), "Default"))
{
Scheme2 = false;
Scheme1 = true;
}
if(GUI.Button(new Rect(50,120, 200, 50), "Alternate"))
{
Scheme1 = false;
Scheme2 = true;
}
}
}
It's worth mentioning that manually changing a Rigidbody's velocity is very rarely warranted. You are interfering with the normal physics calculations by overwriting their natural values with your own -- there is a reason for AddForce and Force$$anonymous$$odes.
One reason for this being that regardless of your input, you are setting its Y component of velocity every physics update. That means that whatever gravitational influence Unity has calculated for your character, your are erasing it with your own.
If there is no input by you (the player), then you are assigning a Zero vector for its physical velocity. How quickly do you expect that to fall? As for the slow speed, this can either be from Input's being floats and your input axis slowly fading to zero or Unity's acceleration still factoring in slightly (which you did not change).
Your answer
Follow this Question
Related Questions
getComponent().Velocity always returns 0? 2 Answers
Proper way to set a RigidBody's maximum velocity 1 Answer
Slow down a character while they are midair while keeping their original velocity 0 Answers
Check/Change velocity of an object 0 Answers
Make a "Balloon" that floats upwards but slows as it gets higher 1 Answer