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