- Home /
Bouncing Ball Game
Hi I´m a noob in Unity and I´m trying to figure out the bounce of object such as a ball, I´m crating a small game with springboards with different bounce force. Such as Springboard1 has the bounce of 4, springboard2 has the bounce of 8 and springboard3 has the bounce of 12. The springboard has to push the player with these different kinds of forces. I´ve tried everything and the ball does gain extra force but just bounces upwards. Anyone can help I´ve tried this code but no avail.
var snapjump : int = 8;
var microjump : int = 2;
var superjump : int = 13;
function OnCollisionEnter (other : Collision) {
if(other.gameObject.tag == "snapjump") {
rigidbody.velocity = Vector3.up * snapjump;
}
if(other.gameObject.tag == "microjump") {
rigidbody.velocity = Vector3.up * microjump;
}
if(other.gameObject.tag == "superjump") {
rigidbody.velocity = Vector3.up * superjump;
}
}
As you can see in the screenshot the springboards are purple but with different kind of tags.
in the first line of the collision function , type
print("other.gameObject.tag = " + other.gameObject.tag);
then you should be able to see the tag, if it is correct, if you have actually assigned the tags or not. Try this.
also, could you please edit your question so the script is formatted correctly ? thx.
Hello @gisli , please post replies in the 'comments' area , don't post your reply as a new answer. You can add comments here and delete your 'answers'.
With the problem, your ball is probably going too fast for the collision to be detected. For example, on one frame-loop the ball is not touching the wall/springboard , but on the next frame-loop the ball has passed the wall , so the collision isn't detected .
There are 2 things to look at : raycasting from your ball , or making changes to the Time $$anonymous$$anager.
Answer by gisli · Mar 25, 2012 at 05:31 PM
The tags are correct and the ball gets bounced correctly now with these codes
var snapjump : int = 10;
var microjump : int = 4;
var superjump : int = 15;
function OnCollisionEnter (other : Collision) {
if(other.gameObject.tag == "microjump") {
print("other.gameObject.tag = " + other.gameObject.tag);
rigidbody.velocity = transform.up * rigidbody.velocity.magnitude * 1.0;
}
if(other.gameObject.tag == "snapjump") {
print("other.gameObject.tag = " + other.gameObject.tag);
rigidbody.velocity = transform.up * rigidbody.velocity.magnitude * 1.2;
}
if(other.gameObject.tag == "superjump") {
print("other.gameObject.tag = " + other.gameObject.tag);
rigidbody.velocity = transform.up * rigidbody.velocity.magnitude * 1.4;
}
}
But when the ball goes to fast it goest through the objects as you can see in the video Youtube Video
Answer by gisli · Mar 25, 2012 at 08:55 PM
Anyhow to fix this object moving through another object ?
Answer by gisli · Mar 25, 2012 at 08:55 PM
Here is the fixed code and it works as you can see in the video YouTube Video
#pragma strict
var microforceAmount : int = 1;
var snapforceAmount : int = 5;
var superforceAmount : int = 8;
function OnCollisionEnter(playerCol : Collision)
{
if(playerCol.gameObject.tag == "microjump"){
rigidbody.AddForce(rigidbody.velocity.normalized * microforceAmount, ForceMode.Impulse);
}
if(playerCol.gameObject.tag == "snapjump"){
rigidbody.AddForce(rigidbody.velocity.normalized * snapforceAmount, ForceMode.Impulse);
}
if(playerCol.gameObject.tag == "superjump"){
rigidbody.AddForce(rigidbody.velocity.normalized * superforceAmount, ForceMode.Impulse);
}
}
function Update () {
}
Your answer
Follow this Question
Related Questions
How to reduce bounce? 1 Answer
How to slow or stop an object with no gravity 2 Answers
Correcting Directional velocity for a plane 0 Answers