Search Unity

How should I code my gun to shoot?

Discussion in 'Getting Started' started by Gabrieldonley, Dec 11, 2015.

  1. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    And would this be some sort of translate code. I've been trying to be a computer, but it's kind of hard when I'm not sure what code to use! Also is this my projectile script or gun script?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Now you're trying to do that TODO item from the above. You've already done the rest, right?

    This is still the right basic approach. The only thing is that you want to do all this inside the if-block you already have (checking for a mouse click, i.e. step 3 in the first outline above). And in step 2, instead of setting the gun's own transform.rotation, you're going to set the transform.rotation of your newly instantiated projectile.
     
  3. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    Ok, I'm just a bit lost. Could we kind of lay it out in todos. I know we already have some, but like doing what with what script and the like. I've reviewed Quaternion.LookRotation documentation. The gameObject noob thing you added. I changed it to gameObject click just for clarification. And since I have a projectile prefab, I can basically be totally done with my projectile script, right?
     
  4. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    In case this helps, here's my code. I'm almost positive somethings not in place. And this is a script called GunBehavior which is different than my projectileShooting script. It is the same one that instantiates the prefab:
    Code (CSharp):
    1. public GameObject projectilePrefab;
    2.     public Transform target;
    3.  
    4.  
    5.     void WhenProjectileShot() {
    6.         if (Input.GetMouseButtonDown (0)) {
    7.        
    8.             GameObject click = GameObject.Instantiate (projectilePrefab) as GameObject;
    9.             click.transform.position = transform.position;
    10.        
    11.             Camera camera = Camera.main;
    12.             Vector3 p = camera.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));
    13.        
    14.             Vector3 relativePos = target.position - transform.position;
    15.             Quaternion rotation = Quaternion.LookRotation (relativePos);
    16.             transform.rotation = rotation;
    17.        
    18.         }
    19.     }
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.    
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.  
    29.     }
     
  5. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    Oh but i just realized. All that code needs to go in update or else it won't work. I just fixed that. I'll repost my code:
    Code (CSharp):
    1. public class GunBehavior : MonoBehaviour {
    2.  
    3.     public GameObject projectilePrefab;
    4.     public Transform target;
    5.  
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.         if (Input.GetMouseButtonDown (0)) {
    16.  
    17.             GameObject click = GameObject.Instantiate (projectilePrefab) as GameObject;
    18.             click.transform.position = transform.position;
    19.  
    20.             Camera camera = Camera.main;
    21.             Vector3 p = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));
    22.  
    23.         }
    24.  
    25.     }
    26. }
     
  6. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    If it's not running in the update, would it just be outside of start or update. I have a projectile the kind of shoots about 2 inches up and then stays there, and I can spam the button as much as I want, but now I just need it to kind of follow the mouse. I think this is what you already know, but just checking.
     
  7. Gabrieldonley

    Gabrieldonley

    Joined:
    Oct 30, 2015
    Posts:
    57
    Joe, would you know what my problem is? I'm running the quaternions and worldpoints in the update like we mentioned above, but nothing is really changing.