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 /
avatar image
0
Question by Bobyrman123 · Jul 14, 2018 at 02:11 AM · collisioncollision detectionclone

Unintentional Occasional Collision

I'm working on a game where you try not to run into obstacles. I have main issue with it however, and that is that some instantiated clones that the player collides with don't activate OnCollisionEnter() on either script. (I have a different script with OnCollisionEnter() on the player and the obstacle, and neither are activated.) Why is it doing this? Again I don't think it's an issue with a single piece of code, seeing as I have two separate scripts with OnCollisionEnter(), and neither of them are being called.

Thanks a Million!

Player Code: public class Player : MonoBehaviour { public GameObject button;

     public Rigidbody rb;
 
     public MeshRenderer render;
 
     public GameObject particles;
 
     public Material material;
 
     float jumpSpeed = 15;
     float moreGravity = 13;
     float gravityMultiplier = 6;
     float jumpLength = 0;
     float torque=1000;
 
     public bool jump = false;
     public bool gamePlaying = true;
     bool madeFaster = false;
     bool touchingGround = true;
 
     private void Start()
     {
         material.color = Random.ColorHSV();
         render.enabled = true;
         Physics.gravity = new Vector3(0, -9.81f * gravityMultiplier, 0);
     }
     void Update ()
     {
         if (gamePlaying == true)
         {
             if (touchingGround == true)
             {
                 if (jumpLength <= 8)
                 {
                     if ((Input.anyKey && button.GetComponent<Button>().touchingButton == false)  || jump)
                     {
                         if (jumpLength == 0)
                             rb.AddTorque(-1*torque,0f, 0f);
                         rb.velocity = new Vector3(0,1,0)* jumpSpeed;
                         jumpLength++;
                     }
                     else if (jumpLength > 0)
                         touchingGround = false;
                 }
                 else
                 touchingGround = false;
 
             }
             if (touchingGround == false)
             {
                 if (rb.velocity.y < -0.1 && madeFaster == false)
                 {
                     Physics.gravity = new Vector3(0, moreGravity * -1 * gravityMultiplier, 0);
                     madeFaster = true;
                 }
             }
         }
 
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         touchingGround = true;
         if (collision.gameObject.tag == "Fence" || collision.gameObject.tag == "Rock" || collision.gameObject.tag == "Stump")
         {
             gamePlaying = false;
             Colapse();
         }
         if (collision.gameObject.name == "Ground")
         {
             jumpLength = 0;
             Physics.gravity = new Vector3(0, -9.81f * gravityMultiplier, 0);
             madeFaster = false;
             rb.rotation = Quaternion.identity;
         }
 
     }
     void Colapse()
     {
         render.enabled = false;
         Quaternion rotation = new Quaternion();
         rotation.x = 0;
         rotation.y = 0;
         rotation.z = 0;
         Destroy(Instantiate(particles, rb.position, rotation), 2);
     }
 }

Cloned Obstacle Code: public class ObsticleClone : MonoBehaviour {

     public GameObject obsticle;
     public GameObject particles;
     
     public bool changedScore = false;
 
     float time;
     public float speed;
 
     private void Start()
     {
          particles = obsticle.GetComponent<Obsticle>().particles;
         speed = obsticle.GetComponent<Obsticle>().speed;
     }
 
     void Update ()
     {
         if (name == "Obsticle")
         {
             time = obsticle.GetComponent<Obsticle>().time;
             this.gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + speed + (time / 200f));
             if (gameObject.transform.position.z >= 14.17f)
             {
                 obsticle.GetComponent<Obsticle>().clone = true;
                 Destroy(this.gameObject);
             }
             if (gameObject.transform.position.z >= 8.78f && !changedScore)
             {
                 changedScore = true;
                 obsticle.GetComponent<Obsticle>().score++;
             }
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.name == "Player")
             Colapse();
     }
 
     void Colapse()
     {
         Renderer r = this.GetComponent<Renderer>();
         Collider c = this.GetComponent<Collider>();
         c.enabled = false;
         r.enabled = false;
         Destroy(Instantiate(particles, this.gameObject.transform.position, this.gameObject.transform.rotation), 2);
         Destroy(this.gameObject);
     }
 }
 

Again, thank you so much!

Comment
Add comment · Show 4
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
avatar image $$anonymous$$ · Jul 14, 2018 at 03:34 AM 0
Share

...some instantiated clones that the player collides with don't activate OnCollisionEnter()

Are you saying that sometimes it works and sometimes it doesn't?

avatar image Bobyrman123 $$anonymous$$ · Jul 14, 2018 at 04:30 AM 0
Share

Yeah, it will sometimes collide correctly, but sometimes it doesn't.

avatar image $$anonymous$$ Bobyrman123 · Jul 14, 2018 at 04:34 AM 0
Share

Have you tried changing the collison detection on the rigid body to continuous?

avatar image Bobyrman123 $$anonymous$$ · Jul 14, 2018 at 04:50 AM 0
Share

It's also not an issue with changing the collision-detection type to Dynamic or Continuous Dynamic, I tried it already.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jehy · Jul 14, 2018 at 04:54 AM

Do the "ObsticleClone" GameObjects spawn with Colliders? I see you're trying to access a Collider in the "Colapse" method but I can't see them being added to the GameObject anywhere.

Comment
Add comment · Show 4 · Share
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
avatar image Bobyrman123 · Jul 14, 2018 at 09:01 PM 0
Share

Sorry, I forgot to add the ObsticleCloned script. It's right here: public class Obsticle : $$anonymous$$onoBehaviour { public GameObject player; public GameObject particles; public $$anonymous$$aterial particleColor; public GameObject fence; public GameObject rock; public GameObject stump;

     public Rigidbody obsticleRigidbody;
 
     public Text scoreText;
 
     public float speed;
     public float height;
     public float random;
     public float score = 0;
     public float time;
 
     public bool clone = true;
 
     void Update()
     {
         time += Time.deltaTime;
         scoreText.text = score.ToString();
         if (clone == true)
         {
             if (player.GetComponent<Player>().gamePlaying == true)
             {
                 clone = false;
                 instantiation();
             }
         }
     }
 
     void instantiation()
     {
         System.Random rnd = new System.Random();
         height = rnd.Next(1, 4);
         Quaternion q = new Quaternion();
         Color color = new Color(86,66,0);
 
         if(height == 1)
         {
             color.r = 86;
             color.g = 66;
             color.b = 57;
             color.a = 0;
             GameObject clone = Instantiate(stump);
             clone.transform.position = new Vector3(0.94f, -1.16f, -11.3f);
             clone.name = "Obsticle";
             clone.tag = "Stump";
         }
         else if (height == 2)
         {
             color.r = 175;
             color.g = 175;
             color.b = 175;
             color.a = 0;
             GameObject clone = Instantiate(rock);
             clone.transform.position = new Vector3(0.27f, -0.3596388f, -11.3f);
             clone.name = "Obsticle";
             clone.tag = "Rock";
         }
         else
         {
             color.r = 231;
             color.g = 143;
             color.b = 49;
             color.a = 0;
             GameObject clone = Instantiate(fence);
             clone.transform.position = new Vector3(-0.84f, -0.73f, -11.3f);
             clone.name = "Obsticle";
             clone.tag = "Fence";
         }
         particleColor.color = color;
     }
 }
 
avatar image Jehy Bobyrman123 · Jul 15, 2018 at 12:50 AM 0
Share

Since none of your collisions are working (and I can't see the inspector), I think your objects don't have Collider components on them. Do you have any Collider type components on your GameObjects: box colliders, capsule colliders, mesh colliders, etc?

avatar image Bobyrman123 Jehy · Jul 15, 2018 at 05:42 AM 0
Share

Yes, everything that needs to collide has a collider on it (Either mesh or box). Again, they do work like 50% of the time, but they don't work the other 50%. It's also not because I'm not instantiating the collider sometimes, I checked: It always makes a collider. It's just for some reason, the event OnCollisionEnter() and the physics engine doesn't register them colliding sometimes.

Show more comments

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

142 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 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

NavMesh Collision Detection? 0 Answers

Collision Dection on Tigger Enter 1 Answer

Last collision 1 Answer

How to detect collision on only one side of an object? [C#] 4 Answers

Best collision detection method? 2 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