How to smash a ball? 2D (C#)
Hello, sorry I'm new in programming. But I would like to make a simple game, baseball game-like. I would like to smash the ball (2D) to make it fly. But my bat is not even smashing the ball, it's just pushing it away :( pleaaase help. Any help is really appreciated :D Thank you in advance :D.
public float RotSpeed = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.D)) {
transform.Rotate(0, 0, RotSpeed * -1);
}
if(Input.GetKey(KeyCode.A)) {
transform.Rotate(0, 0, RotSpeed);
}
}
}
`
decrease the mass of the ball, and increase the mass of the bat, it should increase the effect of hitting it
Answer by Dibbie · Nov 06, 2015 at 05:47 PM
In my personal opinion, anything with physics, especially starting out, can be advanced. Look into RigidBody2d, and Collider2D (Im not sure if you are using a BoxCollider2d, or a SphereCollider2D or another type of Collider2d).
The reason I say to look into these, is because it can be hard to explain the entire process to fixing your problem especially without actually seeing exactly how your physics are reacting, the code doesnt help very much at all, because it only shows how your rotating an object, that doesnt even seem like it may have any reference to any physics.
RigidBodies contain weight, as well as gravity settings, the project itself may even have settings, and you can add stuff like physics materials which can do some pretty interesting stuff (in my opinion), like have whatever object bounce off of another, or slide off another, or react like it would if it hit wood or ice, or you can even set up your own. It can get complicated, cause Physics in general, can get complicated. So for sure look into those 2 elements.
For starting out, you may want to consider maybe trying a more simplistic project, or if you are really passionate about this one, maybe look on YouTube for some tutorials on Physics and how they work in Unity, and try to replicate them, to get a understanding of it.
Thank you sir :D I already watched many unity and program$$anonymous$$g tutorials on youtube, but really can't find anything. I was looking for a scripting tutorial that spins an object using force. Because I think, if I used force in rotating it, it would be able to smash the ball. Because i think, using transform.Rotate is just teleporting the plank's rotation :o Thank you sir :D
Youll probably see some problems using force, cause its continuous and I found accumulative if you donno how to use it right - for rotating, maybe look into Slerp. Itll allow you to rotate an object over time.
Your answer
Follow this Question
Related Questions
How to learn loading/pressure/compression on a 2D body? 0 Answers
Physics2D.OverlapBox (C#) 0 Answers
How can I isolate just the character to slow down 0 Answers
My character flies upwards instead of jumping (2D) 1 Answer
Is calling OnCollisionEnter2D on allot of GameObjects leading to worse performance? 0 Answers