- Home /
How to apply relative force to Ball using another object?
I am currently developing Glow hockey type game, My problem is that how can i apply force to Ball relative to hitting speed of Paddle.
I have create paddle with rigidbody and Bouncy PhysicsMaterials I have Ball With rigidbody and Bouncy PhysicsMaterials
My Code is:
function Update () {
if(Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, rayCastHit))
{
transform.position.x = rayCastHit.point.x;
transform.position.y = rayCastHit.point.y;
}
}
}
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag == "Ball") {
var temp = collision.contacts[0].normal;
if(temp.x < 0)
collision.gameObject.rigidbody.AddForce(1000, 0, 0);
else if(temp.x >0)
collision.gameObject.rigidbody.AddForce(-1000, 0, 0);
if(temp.y < 0)
collision.gameObject.rigidbody.AddForce(0, 1000, 0);
else if(temp.y >0)
collision.gameObject.rigidbody.AddForce(0, -1000, 0);
}
}
My Problem is What Parameter should I consider to apply force on Ball without hardcode 1000
I don't think you should need to apply any force to the ball directly really, if it's a rigidbody and the paddle is a rigidbody the act of them colliding should effect the ball anyway. If you really need to apply forces directly to the ball you could consider the paddles current velocity (rigidbody.velocity) as a factor in how much to effect the ball.
thank you Phles, since you said that doesn't require AddForce to Ball While they collide,
if yes, then i also tried it but ball speed was to much slow,
if addforce is necessary, and i would set 1000 force to ball when they collide, would it generate same velocity each time or it depends how fast paddle hit the ball.
i had moved my paddle with ray and $$anonymous$$ouse Button and its rigidbody.velocity was all time zero. how should i go about it?
if you tried it and the ball moved too slow then lower the balls mass property, in reality a ball would be much lighter than the paddle hitting it.