AddForce only working inside another object's collider
Hello everyone, this is my first post in this forum, so thank you for understanding, any comments are welcome, and I will be very thankful for help, because I'm stuck on a very strange problem at the beginning of a very fun project. And my son's passion for programming is at stake!
I am writing the first, basic functionality in a football game - scanning the world around a player and kicking the ball in the desired direction. There is a slab of a field, some capsules for ballers, two goals and a primitive sphere for a ball. The ballers have enlarged colliders around them which are used for finding their "catchment areas" with a raycast. The ball, of course, has a rigidbody and a collider. I have a script for turning the baller around and scanning with a raycast, and a script for the ball.
The ball is held by the active baller using his own transform + forward. Or by childing the ball to him, makes no difference. When the baller wants to kick the ball, the script unchecks isKinematic (or also unchilds it from the baller - again the result is the same) and performs AddForce on the ball. The result is the same whether the ball kicks itself or the AddForce is in the baller's script. AddForce can use the baller's transform.forward, that of the ball, or an absolute Vector3, doesn't matter.
Here is where the funny part begins. The ball flies nicely until it gets out of the baller's large (5x the size of baller) collider, then it suddenly drops, sliding on the outside of the collider. Better yet, If I do anything so that the ball is not within the baller's collider when kicked, or it cannot interact with it (such as uncheck collider in inspector, reduce the collider to the size of the baller, move it to a different, non-colliding layer, make it a trigger), the ball, after it is released from its transform, doesn't react to AddForce at all, it just drops dead to the ground.
None of this happens if the ball is never held by a transform, just lies there - then it flies perfectly to the goal. But I need to hold the ball in position to keep it by the baller and kick it where I want!
So, the question is - what does the baller's collider even have to do with the ball's movement, so that the ball is not pushed by AddForce if it is not in interaction with the baller's collider? How do I make it work? I'm totally puzzled here.
Now, let's see if I can past my code here Sorry to have my vars in a mix of Polish and English, I'm doing this with my son :)
The ball's script:
public class pilka : MonoBehaviour {
public bool ballowned;
GameObject ballowner;
void Start () {
ballowner = GameObject.Find("Gracz");
ballowned = true;
Vector3 naziemi = new Vector3 (0f, -0.4f, 0f);
transform.position = ballowner.transform.position + ballowner.transform.forward - naziemi;
}
void FixedUpdate () {
skanowanie skanskrypt = ballowner.GetComponent<skanowanie> ();
Rigidbody rb = GetComponent<Rigidbody> ();
if (skanskrypt.kicktrigger) { //the ball script receives a bool trigger from the baller's scanning script
transform.parent = null;
rb.isKinematic = false;
Vector3 sila = new Vector3(0, 1); //originally, there was a transform.forward here, but I removed it to get rid of as many links to the baller object as I could
rb.AddForce (sila);
skanskrypt.kicktrigger = false;
ballowned = false;
}
if (ballowned)
transform.parent = ballowner.transform;
else
transform.parent = null;
}
}
And the script for the baller, scanning and triggering the kick:
public class skanowanie : MonoBehaviour {
public bool kicktrigger = false;
void Update () {
GameObject ball = GameObject.Find ("Pilka");
pilka pilkascript = ball.GetComponent<pilka>();
RaycastHit hit;
Ray promien = new Ray (transform.position, transform.forward);
if (Physics.Raycast(promien, out hit, 100))
{
if (hit.collider.tag == "Przeciwnik")
{Debug.DrawRay (transform.position, transform.forward*100, Color.red);}
else if (hit.collider.tag == "Player")
{Debug.DrawRay (transform.position, transform.forward*100, Color.blue);}
else if (hit.collider.tag == "Finish")
{Debug.DrawRay (transform.position, transform.forward*100, Color.green);
if (pilkascript.ballowned == true) kick();} //and here's the kicker!!!
else Debug.DrawRay (transform.position, transform.forward*100, Color.black);
}
else Debug.DrawRay (transform.position, transform.forward*100, Color.white);
}
public void kick()
{
kicktrigger = true; //the kick signal picked up by the ball script
}
}
Thanks for any help, IOU1!
Your answer
![](https://koobas.hobune.stream/wayback/20220612093315im_/https://answers.unity.com/themes/thub/images/avi.jpg)