How do I make a 2D player controlled ball roll when moving?
Hey everyone, I'm really new to scripting, and I'm trying to make a 2D game where you play as a ball. I've looked at a bunch of tutorials and I've now gotten a couple of scripts, one of which is called CharacterController2D, and another one which is just called PlayerMovement. These scripts are based on Brackey's Youtube video "2D Movement in Unity (Tutorial)". Now, despite writing some of the code myself with the help of tutorials, my knowledge of what it all actually means is pretty limited. I've managed to work out, with the help of Google, that, ideally, I need to use something called AddForce (or maybe even AddTorque), but I can't for the life of me figure out where that goes in my scripts, or if it even works for my particular ones.
Currently, my ball only rolls with the help of the friction of the material which is attached to my player character. The problem is that this makes turning directions quickly look really unnatural, as well as enables it to climb walls.
If anyone could help me by looking at my code and telling me, preferably in as much detail as you have the energy to, how and where to use AddForce, rotation or whatever else (or if I should write a new script), I swear to God I'll include you in my will. Seriously, it's been days and I'm running out of toilet paper. Please help.
Also, if I'm accidentally breaking some social rules in my post or something I apologize, this is my first venture into the Unity community.
I also realized now that I can only upload 2 images, and while one of the scripts fits into one image, with the other one, I'm uploading the portion that I think is most likely to be relevant. If you need more, let me know! I suppose I could paste the code into this description, but it looks pretty messy, so I'll go with the images to begin with.
Again, if anyone could spare the time, I would very much appreciate it!
Answer by opritaoctav · Oct 05, 2021 at 03:45 PM
mine public class playerController : MonoBehaviour { public Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void FixedUpdate() { if (Input.GetKey(KeyCode.D)) rb.velocity = new Vector2(15f, rb.velocity.y); else if (Input.GetKey(KeyCode.A)) rb.velocity = new Vector2(-15f, rb.velocity.y); else rb.velocity = new Vector2(0, rb.velocity.y); } }
Your answer
Follow this Question
Related Questions
Set an Object's 2d physics material in c# 0 Answers
Ball Speed is not increasing as per code 0 Answers
Trying to make a Parkour Platformer! 0 Answers
2D Top-Down Character: How to make my character move using physics? 1 Answer
why does my game lags? 0 Answers