0

I have to scripts the first is PlayerController which is attached to the parentobject and shooting which is attached to the childobject.

I want to make it so that if the player is moving the shooting animation cant be activated. Keep in mind they are separate scripts.

PlayerController movement:

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;
    }
}

Here is my Shooting script:

public class Shooting : MonoBehaviour
{
public float Damage = 10.0f;
public float Range = 100.0f;

public Camera cam;
public ParticleSystem MuzzleFlash;
Animator anim;
// Start is called before the first frame update
void Start()
{
    anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{

    Animation();
    if (Input.GetButtonDown("Fire1"))
    {
        Shoot();
    }
}

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);
        }

    }
}

void Animation()
{
    if (Input.GetButtonDown("Fire1"))
    {
        anim.SetBool("IsShooting", true);
    }


    if (Input.GetButtonUp("Fire1"))
    {
        anim.SetBool("IsShooting", false);
    }
}

}

Ethan K-B
  • 115
  • 12
  • 3
    Possible duplicate of [Unity how do I make my gun stop shooting when the Player is moving?](https://stackoverflow.com/questions/57225665/unity-how-do-i-make-my-gun-stop-shooting-when-the-player-is-moving) – Ruzihm Jul 26 '19 at 20:34
  • @Ruzihm Yes, but in this case I rephrased my question and it will be a fresh start. I am keeping the other one because people put work into it. – Ethan K-B Jul 26 '19 at 20:34
  • 1
    "a fresh start" when the question hasn't changed substantially only means that effort is split between two places instead of accumulating in one easy to view place. This question should be closed unless the question has changed substantially. – Ruzihm Jul 26 '19 at 20:49

1 Answers1

1

Keep track of if you are moving in your PlayerController script (public bool moving). Check if you are moving before playing the animation with GetComponentInParent.

PlayerController

public bool moving;
void Movement()
{
    moving = false;
    if(Mathf.abs(Input.GetAxis("Vertical")) > 0.0001f || Mathf.abs(Input.GetAxis("Horizontal")) > 0.0001f) moving = true;
    ...

Shooting

...
void Animation()
{
    if (Input.GetButtonDown("Fire1") && GetComponentInParent<PlayerController>().moving == false)
    {
        anim.SetBool("IsShooting", true);
    }


    if (Input.GetButtonUp("Fire1"))
    {
        anim.SetBool("IsShooting", false);
    }
}
R Astra
  • 469
  • 3
  • 6
  • @R Astra I had to move the && GetComponent to the code in update and change it to true and it works perfectly. – Ethan K-B Jul 26 '19 at 21:28
  • It's extremely inefficient to call `GetComponent` multiple times .. better rather do it only once in `Awake`, store the result and reuse the reference – derHugo Jul 27 '19 at 09:01
  • And why `> 0.0001f` and not simply `> 0`? – derHugo Jul 27 '19 at 09:03
  • GetComponent generally seems to perform ok (not noticable performance impact) if you only use it once per frame, so for this case I didn't reuse the result. The performance problems generally appear when you use it every frame in a for loop. >0 should work, but I put >0.0001f just in case (perhaps unnecessary). – R Astra Jul 27 '19 at 12:20