In the game, there is a ball, and moving objects that can hit it. I want the ball to move when an object hits it. But the ball should move to the right direction, the one which the collision was from. I looked for some answers on the internet, however, most codes were in Java script, and those that were in C# weren't helpful because it needed a Rigidbody. The ball is a rigidbody, but the objects that may hit it, aren't. Those are being moved by the CharacterController. The question is how can I detect the collision, to know from what direction it came, and move it to that direction. Thank you.
Use rigidbody.AddForce instead of transform.Translate so that the collisions are detected. For the direction create a variable with the difference between the target and the object itself and then normalize to find out the direction.
I think a normalized vector from the ball to the object.
direction = (ball.transform.position - kicker.transform.position).normalized;
I made a game doing kind of this thing you are doing. (source code link in the post) http://luigigarcia.byethost7.com/projects/goal-a-r-goal/
I think that your kicking object must do this work, not the ball. You need to know the ball someway:
- reference set in some component;
- through the OnCollision MonoBehaviour methods;
You apply the direction with something like this:
ball.theRigidbody.AddForce(direction * powerValue);