Search Unity

Shooting system true or false

Discussion in 'Getting Started' started by phantomunboxing, Feb 10, 2016.

  1. phantomunboxing

    phantomunboxing

    Joined:
    Nov 10, 2015
    Posts:
    43
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It'd be easier to help you if you pasted your code right here (using the "Insert Code" button, between the movie filmstrip and the floppy disk icon) rather than in Pastebin.

    But anyway, maybe the difficulty is that you don't know how to access the Character.CarryingWeapon property from your ProjectileShooter script? The easiest way to do that is with GetComponent. Something like this:

    Code (CSharp):
    1.     void Update () {
    2.         timeShots += Time.deltaTime;
    3.         bool hasWeapon = GetComponent<Character>().CarryingWeapon;
    4.         if (Input.GetMouseButtonDown(0) && timeShots >= timeBetweenShots && hasWeapon) {
    5.             timeShots = 0;
    6.             GameObject projectile = Instantiate(prefab) as GameObject;
    7.             projectile.transform.position=transform.position+Camera.main.transform.forward * 2;
    8.             Rigidbody rb = projectile.GetComponent<Rigidbody>();
    9.             rb.velocity=Camera.main.transform.forward * 40;
    10.         }
    11.     }