Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SirIVLucifer · Apr 23 at 07:52 PM · timeoncollisionenterdelay

Oncollisionenter and time,OnCollisonEnter and time.

I'm making a cube collision game. If the cubes are the same, they collide and turn into another cube with 2. My goal is for the cubes to transform after a certain amount of time after colliding. I couldn't do what I did. Can you help me? thanks

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class CubeCollision : MonoBehaviour
 {
     Cube cube ;
   // public AudioSource audio;
 
     private void Awake() {
         cube = GetComponent<Cube>();
        // audio = GetComponent<AudioSource>();
     }
            IEnumerator WaitOneSecToDeactivateTrash(Collision collision)
       {
           Debug.Log("hello before time");
           yield return new WaitForSeconds (1f);
           Debug.Log("hello after time");
       }
     public void  OnCollisionEnter(Collision collision) {
         Cube otherCube = collision.gameObject.GetComponent<Cube>();     
         // check if contacted with other cube
         if(otherCube != null && cube.CubeID > otherCube.CubeID){
             //check if both cubes have same number
             if(cube.CubeNumber == otherCube.CubeNumber){
                 //Debug.Log("HIT: "+cube.CubeNumber);
                 Score.score +=cube.CubeNumber*2;
                Vector3 contactPoint = collision.contacts[0].point; 
                //check if cubes number less than max number in cubespawner:
                 if(otherCube.CubeNumber < CubeSpawner.Instance.maxCubeNumber){
                      //spawn a new cube as a result.
                      Cube newCube = CubeSpawner.Instance.Spawn(cube.CubeNumber*2, contactPoint+Vector3.up*.3f); //2 4 8 16 32 64 128 256 512 1024 2048 4096                           
                     //push the new cube up and forward.
                      float pushForce = 2.5f;
                      newCube.CubeRigidbody.AddForce(new Vector3(0, .3f, 1f)*pushForce,ForceMode.Impulse);
                      //add some torque
                      float randomValue = Random.Range(-20f,20f);
                      Vector3 randomDirection = Vector3.one * randomValue;
                      newCube.CubeRigidbody.AddTorque(randomDirection);            
                 }
                 //the expolison should effect sorrounded cubes too.
                 Collider[] surroundedCubes = Physics.OverlapSphere ( contactPoint,1f);
                 float expolisonForce = 400f;
                 float expolisonRadius = 1.5f;
                 foreach(Collider coll in surroundedCubes){
                     if(coll.attachedRigidbody!= null){
                         coll.attachedRigidbody.AddExplosionForce(expolisonForce,contactPoint,expolisonRadius);
                     }
                 }
                 FX.Instance.PlayCubeExplosionFX(contactPoint,cube.CubeColor);
                 //Destrory the two cubes: 
                 CubeSpawner.Instance.DestroyCube(cube,3f); //sonradan incele ---------!!!
                 CubeSpawner.Instance.DestroyCube(otherCube,3f);
                  
             }
         }
 
     }
 }
 


, I'm making a cube collision game. If the cubes are the same, they collide and turn into another cube with 2 times. My goal is for the cubes to transform after a certain time after they collide. I couldn't do it no matter what I did. can you help me? thanks

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class CubeCollision : MonoBehaviour
 {
     Cube cube ;
   // public AudioSource audio;
 
     private void Awake() {
         cube = GetComponent<Cube>();
        // audio = GetComponent<AudioSource>();
     }
            IEnumerator WaitOneSecToDeactivateTrash(Collision collision)
       {
           Debug.Log("hello before time");
           yield return new WaitForSeconds (1f);
           Debug.Log("hello after time");
       }
     public void  OnCollisionEnter(Collision collision) {
         Cube otherCube = collision.gameObject.GetComponent<Cube>();     
         // check if contacted with other cube
         if(otherCube != null && cube.CubeID > otherCube.CubeID){
             //check if both cubes have same number
             if(cube.CubeNumber == otherCube.CubeNumber){
                 //Debug.Log("HIT: "+cube.CubeNumber);
                 Score.score +=cube.CubeNumber*2;
                Vector3 contactPoint = collision.contacts[0].point; 
                //check if cubes number less than max number in cubespawner:
                 if(otherCube.CubeNumber < CubeSpawner.Instance.maxCubeNumber){
                      //spawn a new cube as a result.
                      Cube newCube = CubeSpawner.Instance.Spawn(cube.CubeNumber*2, contactPoint+Vector3.up*.3f); //2 4 8 16 32 64 128 256 512 1024 2048 4096                           
                     //push the new cube up and forward.
                      float pushForce = 2.5f;
                      newCube.CubeRigidbody.AddForce(new Vector3(0, .3f, 1f)*pushForce,ForceMode.Impulse);
                      //add some torque
                      float randomValue = Random.Range(-20f,20f);
                      Vector3 randomDirection = Vector3.one * randomValue;
                      newCube.CubeRigidbody.AddTorque(randomDirection);            
                 }
                 //the expolison should effect sorrounded cubes too.
                 Collider[] surroundedCubes = Physics.OverlapSphere ( contactPoint,1f);
                 float expolisonForce = 400f;
                 float expolisonRadius = 1.5f;
                 foreach(Collider coll in surroundedCubes){
                     if(coll.attachedRigidbody!= null){
                         coll.attachedRigidbody.AddExplosionForce(expolisonForce,contactPoint,expolisonRadius);
                     }
                 }
                 FX.Instance.PlayCubeExplosionFX(contactPoint,cube.CubeColor);
                 //Destrory the two cubes: 
                 CubeSpawner.Instance.DestroyCube(cube,3f); //sonradan incele ---------!!!
                 CubeSpawner.Instance.DestroyCube(otherCube,3f);
                  
             }
         }
 
     }
 }
 

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

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

Mining in RTS 0 Answers

How do I delay a function to match the duration of a sound clip? 0 Answers

Hit play takes a long time to start 1 Answer

prefab time delay 1 Answer

I would like to add a delay to activating a camera 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