Hello, Can someone please explain to me why I could not use rb.velocity twice in my code. When I run my game, the player object cannot jump due to movement using rb.velocity. When I change movement to MovePosition or AddForce, it works fine. I’m just curious to why it wouldn’t work if I used rb.velocity for both Jump and movement. Here is my code. Code (CSharp): public float jumpForce; //set jump's strength. public float speed; //how fast the object move. private Rigidbody rb; //store rigidbody component void Start () { rb = GetComponent<Rigidbody>(); //Get Rigidbody component } void FixedUpdate () { //Jump if (Input.GetButton("Jump") && isGrounded()) { rb.velocity = new Vector3(0, jumpForce, 0); } //Movement float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical); rb.velocity = movement * speed; } Thanks in advance.
The problem is that you're overwriting the velocity with your 'movement' variable. Try changing the 'y' portion of movement so it's "rb.velocity.y".
Forces apply acceleration on your rb's Acceleration changes velocity over time To set the velocity to a specific value negates your effects of accelerate or add forces, but also all previous set velocity values. As methos already said you overwrite your velocity result in your script. So the first if statement can never be used in the running game.
Thanks! I notice when I did changed y in ‘movement’ variable to rb.velocity.y, it was also being calculated by speed; which did produce some interesting result. Of course, larger numbers send the player object skyward immediately, but smaller numbers gave it nice quick bounces when holding the space key. Once again, thanks you guys for clearing that up for me.