Hey, so I am completely new to Unity, I only know Native Android Dev, Java, PHP, JS and other basic things, but Unity is something new, I want to create small game as I learn it as I believe it’s one of the best ways to learn.
Anyways, I am trying to make a game where user controls bouncing ball to move around screen, so far I got to:
-Ball spawns and only moves along X axis with user controls (left/right), and when user lets the key go it will start stopping and when reaching velocity of less than 0.03 in any direction it’s stopped completely to avoid endless rolling.
Here is the code I wrote:
void Update () {
Rigidbody ball = GetComponent<Rigidbody> ();
Vector3 curr_velocity = ball.velocity;
if (Input.GetButton ("Horizontal")) {
//gameObject.transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * 0.05f);
//gameObject.transform.Rotate(new Vector3(15, 180, 180));
//ball.transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * 0.05f);
ball.AddForce (Vector3.right * Input.GetAxis ("Horizontal") * 15);
//ball.transform.Rotate (new Vector3 (15, 180, 180));
}
else{
// if(OnCollisionEnter("Line01") == true){
Vector3 multiplier = new Vector3(0.97f,1,1);
ball.velocity = Vector3.Scale(curr_velocity, multiplier);
if(Mathf.Abs(curr_velocity.x) < 0.03){
ball.velocity = Vector3.zero;
ball.angularVelocity = Vector3.zero;
}
// }
}
}
Works fine, but I am wondering is this the way it’s done? or is there some kind of way/functionality in Unity physics engine that would do this? or does it always need to have some force applied to make objects move?
If Yes:
How would I make it bounce when ball lands? I don’t want it to look robotic to land and kind of stick to ground straight away, I want it to bounce off a bit until it settles down.
If No:
How is it done then?
Also side question: I want ball coming to stop only when it’s touching ground (platform in my case tagged as “Ground”) but when it goes into free fall it would increase velocity as it falls and then use the velocity to apply various forces to simulate free fall, right now if it’s falling it just sticks and falls straight (since the code says: velocity.x = 0, then stop it completely).
Question: How do I check if my ball is touching ground(a.k.a - Platform or “Ground” )
Thanks a lot, sorry for such basic questions, you will probably see me around asking a lot, I usually google things but sometimes I can’t find decent enough answer.
P.S - Why do I see so many examples online being given in JavaScript but not C#? are most people making games in JS?