Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Keep gravity while adding velocity to a Rigidbody?

$
0
0
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!

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>