Reputation: 127
I am trying to make a game where you bounce a ball up and down on a platform. I added a rigidbody component to the ball, making the ball "Use Gravity", as well as have material in the SphereCollider so it will have "bounciness" or a coefficient of restitution. The ball follows gravity nice and dandy on a stationary platform/plane.
After, I tried adding the platform component. This component, by the way, simply moves up and down on the Y-axis (as if someone is just bouncing a ping pong ball up and down on a paddle). To make it go up, I used this C# script.
if (Input.GetKeyDown(KeyCode.W))
{
heldDownW = true;
}
if (Input.GetKeyUp(KeyCode.W))
{
heldDownW = false;
}
if (heldDownW)
{
transform.position = new Vector3(transform.position.x, (transform.position.y + 1f), transform.position.z);
}
This simply checks if the W key is being held down, move the platform up. I have the same thing for down on the S key. The platform moving component works well. However, here is my problem. The ball works fine on a stationary platform, or when I am not pressing the W or S key. It bounces and obeys gravity. However, when I am moving the platform while the ball is in motion, the ball goes straight through the platform.
I think I know the reason why- it is the transform.position. It is simply moving the platform even though the ball is still in the air, thus changing the position of the platform right through the ball. However, I am not sure how to proceed from here. I tried using OnCollisionEnter, but I don't know what to put in the OnCollisionEnter method because the gravity/collision components are not coded in a script. I want the ball to bounce on the platform regardless of the platform moving or not. Any help would be greatly appreciated. Thank you.
Upvotes: 0
Views: 1737
Reputation: 151
You will have to attached a Rigidbody to that platform thing, then set the 'isKinematic' field to 'true' in the inspector.
Then you will need to use the RidigBody.MovePosition() instead of transform.position
if(heldDownW)
{
rigidbody.MovePosition(new Vector3(transform.position.x, (transform.position.y + 1f), transform.position.z));
}
Also, based on what I read, it will be also nice to put that if-block to FixedUpdate() instead, because FixedUpdate() are for physics related stuff.
void Update()
{
if(Input.GetKeyDown(KeyCode.W))
{
heldDownW = true;
}
if(Input.GetKeyUp(KeyCode.W))
{
heldDownW = false;
}
}
void FixedUpdate()
{
if(heldDownW)
{
rigidbody.MovePosition(new Vector3(transform.position.x, (transform.position.y + 10f * Time.deltaTime), transform.position.z));
}
}
Another thing is to use Time.deltaTime when your are doing speed calculation :
if(heldDownW)
{
rigidbody.MovePosition(new Vector3(transform.position.x, (transform.position.y + 10f * Time.deltaTime), transform.position.z));
}
by using Time.deltaTime you are like saying 'move 10 meters per second upwards' if you dont use Time.deltaTime, you are moving it 10 meters per frame which is not you what you want.
Upvotes: 1