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?

1 Answer

1

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));
    }
}

Thank you very much, I see my problem now.. :)