Keep gravity while adding velocity to a Rigidbody?
Little help for a newbie. So I have a player "sphere" which moves around on the x using addforce with the a and d keys. I tried to implement a jumping feature, but I have this issue where the ball, if force is added on the x on during the fall, will seem to be unaffected by gravity and move only to the side where velocity is added. Here is the code:
using UnityEngine;
using System.Collections;
public class movementx : MonoBehaviour {
public int speedleft;
public int speedright;
public int jump;
public int downjump;
public Rigidbody ball;
void Update () {
if (Input.GetKey (KeyCode.D)) {
ball.velocity = Vector3.zero;
ball.angularVelocity = Vector3.zero;
ball.AddForce (speedleft, 0, 0);
} else if (Input.GetKey (KeyCode.A)) {
ball.velocity = Vector3.zero;
ball.angularVelocity = Vector3.zero;
ball.AddForce (speedright, 0, 0);
} else if (Input.GetKeyDown (KeyCode.Space)) {
ball.velocity = Vector3.zero;
ball.angularVelocity = Vector3.zero;
ball.AddForce (0, jump, 0);
}
}
}
Thanks again for the help!
Comment
Best Answer
Answer by MrMeows · Oct 26, 2015 at 03:50 AM
Remove this:
ball.velocity = Vector3.zero;
ball.angularVelocity = Vector3.zero;
Also, you will need to reduce speedleft, speedright, and jump.