- Home /
Propelling a Ball
Hey,
I have a player and i want it to realistically mimic shooting a ball when they collide.
What I tried is
public void OnControllerColliderHit(ControllerColliderHit other)
{
if (other.gameObject.tag == "ball")
{
Vector3 direction = new Vector3(-myTransform.position.normalized.x * 100, 0, -myTransform.position.normalized.z * 100);
ball.GetComponent<Rigidbody>().AddForce(direction);
}
}
on the player.
This kinda workes in some circumstances but sometimes it goes in the wrong direction or gets stuck to the player (?).
I'm thinking I approached this all wrong, can anyone point me in the right direction?
Comment
Best Answer
Answer by diddykonga · Mar 17, 2012 at 12:11 AM
Try this :
public void OnControllerColliderHit(ControllerColliderHit other)
{
if (other.gameObject.tag == "ball")
{
float force = 100.0f;
Vector3 direction = (other.transform.position - myTransform.position).normalized;
ball.GetComponent<Rigidbody>().AddForce(new Vector3(direction.x * force, 0, direction.z * force));
}
}
Your answer
Follow this Question
Related Questions
Non-rotating collider fixed to a rolling ball 1 Answer
Avoid one gameObject skin in another gameObject 2 Answers
how to Apply Gravity at Run time ..????????? 2 Answers
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer
The ball getting slow down and slicing. 0 Answers