2

enter image description hereI am making a gun shooting script, but I do not know how to disable shooting when the player is running. Can someone help me with that?

void Shoot()
{
    MuzzleFlash.Play();


    RaycastHit hit;
    if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, Range))
    {
        Debug.Log(hit.transform.name);

        EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
        if (enemy != null)
        {
            enemy.TakeDamage(Damage);
        }

    }
}

Here is the movement section of my character controller script:

void Movement()
{
    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * WalkSpeed * Time.deltaTime);
    transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * WalkSpeed * Time.deltaTime);

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        WalkSpeed = RunSpeed;
    }
    if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        WalkSpeed = DefaultSpeed;
    }
}
Ethan K-B
  • 115
  • 12

2 Answers2

3

You can set variables in other scripts like this:

[Serialize Field]
private GameObject objectWithYourScriptYouNeed; 
private ClassOfScriptYouNeed pS; 


void Start()
{     
    pS = objectWithYourScript.GetComponent<ClassOfScriptYouNeed>();    
    pS.varName = 12345;
}

So, in your shoot script:

Public Bool isMoving;

void Shoot()
{
    //I'm assuming you also dont want muzzleflash to play.
    if (isMoving != true)
    {
        MuzzleFlash.Play();    

        RaycastHit hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, Range))
        {

            Debug.Log(hit.transform.name);

            EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
            if (enemy != null)
            {
                enemy.TakeDamage(Damage);
            }
        }
    }
}

Your movement Script:

//put your shooting scripts name below.
private ShootScript shootScript; 

void start()
{    

    shootScript = GetComponent<ShootScript>();

}

void Movement()
{
    shootScript.isMoving = Input.GetAxis("Vertical") != 0f 
                           || Input.GetAxis("Horizontal") != 0f ; 

    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") 
                        * WalkSpeed * Time.deltaTime);
    transform.Translate(Vector3.right * Input.GetAxis("Horizontal") 
                        * WalkSpeed * Time.deltaTime);

    // GetKey is true on every frame that shift is held down, and false when it isn't
    // GetKeyDown is only true on the first frame in a row that shift is held down    
    if (Input.GetKey(KeyCode.LeftShift))
    {
        WalkSpeed = RunSpeed;
    }
    else 
    {
        WalkSpeed = DefaultSpeed;
    }
}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Laskio
  • 95
  • 1
  • 8
  • Okay that helps a bit but I still dont know how to make the if statement for it. – Ethan K-B Jul 26 '19 at 19:19
  • it just gives me an error with private parentScript = pS; – Ethan K-B Jul 26 '19 at 19:26
  • i am uploading a screenshot of what happens – Ethan K-B Jul 26 '19 at 19:33
  • Invalid token '=' in class, struct, or interface member declaration Invalid token ';' in class, struct, or interface member declaration Invalid expression term ';' – Ethan K-B Jul 26 '19 at 19:42
  • Did you change code? You have an error from mine. It is pC = (PlayerController); – Laskio Jul 26 '19 at 19:45
  • private PlayerController = pC; // pC = (PlayerController); – Ethan K-B Jul 26 '19 at 19:48
  • My bad I believe it is just Private PlayerController pC; .. there is no equals sign. Very hard to do without Unity on hand. – Laskio Jul 26 '19 at 19:49
  • Yeah I know what you mean also this is the error Invalid expression term '<' – Ethan K-B Jul 26 '19 at 19:53
  • 1
    I would suggest looking up some videos on how to reference other scripts variables. I dont want this to get too long and be an annoyance to people. There is plenty of videos, once you have that the other logic should work out if you understand it. Sorry for the inconvenience it is just too hard for me to error check at the moment – Laskio Jul 26 '19 at 20:02
  • If you have not found anything yet, https://stackoverflow.com/questions/25930919/accessing-a-variable-from-another-script-c-sharp This is very helpful. This allows you to grab a script from another game object. – Laskio Jul 26 '19 at 20:29
  • 1
    @EthanK-B I fixed some of the syntax errors in this question. You never mention the names of your scripts' classes so I can't put the exact terms in. Hopefully they make sense. – Ruzihm Jul 26 '19 at 20:38
  • 1
    Thank you again Ruzihm for your awesome work! Hope this solves your question now Ethan. – Laskio Jul 26 '19 at 20:40
  • @EthanK-B I changed the answer to avoid the need for setting `isMoving` back to `false` manually. – Ruzihm Jul 26 '19 at 21:46
2

Make a boolean isMoving and when they're moving set it to true and false when they stop. Then in your void Shoot make an if statement for the moving bool and only fire when it's false.

William V.
  • 343
  • 1
  • 13
  • The only problem is that the character controller script that controls movement is on the parent object while the shooting script is attached to child object. – Ethan K-B Jul 26 '19 at 19:06