Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by esuotfartete · Sep 23, 2016 at 11:44 AM · physicscollideraddforce

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision Detection with DrawMeshInstanced 0 Answers

Unity 2D colliders not triggering,Unity 2D collider not triggering 0 Answers

Ignore collision based on position 1 Answer

Flipping Sprite with Box Collider attached 1 Answer

Cube sinks partially into maze 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges