Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

help in converting from keyinput to collsion script

Discussion in 'Scripting' started by UNITY3D_TEAM, Sep 17, 2012.

  1. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    hi this script is work for my bullet .when i press a button it will clone sphere (one bullet).but i want to change the code like the sphere want to emit after it collsion with other object like cube .please help me to do this:thanks in advance
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class pra : MonoBehaviour{
    6.  
    7. public GameObject gun;
    8. private GameObject bullet=null;
    9. private float power;
    10.  
    11.  
    12. void Start()
    13. {
    14.  
    15. bullet=GameObject.Find("bullet");
    16.  
    17.  
    18. }
    19. void Update()
    20. {
    21. if(Input.GetKeyUp("a"))
    22. {
    23.              power =10;
    24. GameObject obj = Instantiate(bullet, gun.transform.position, gun.transform.rotation) as GameObject;
    25.             obj.rigidbody.velocity = gun.transform.TransformDirection(Vector3.left*power);
    26.             obj.name = "BulletClone";
    27.            
    28.  
    29. }
    30.        
    31.        
    32.        
    33.  
    34. }}
    35.  
     
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Put the instantiation code in OnCollisionEnter instead.