hi guys, i need your help, i am doing a little platform 2d game .I am a beginner and i am trying to refer to the shooting animation, in the script to shoot. if you can help me … Thanks !!
{
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 20f;
private bool IsShooting;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
void Shoot()
{
isShooting = true;
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.right * bulletForce, ForceMode2D.Impulse);
}
private void UpdateAnimations()
{
anim.SetBool("IsShooting", IsShooting);
}
}
also I tried to do this :
{
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 20f;
private bool IsShooting;
public bool isShooting;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
isShooting = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
isShooting = true;
}
UpdateAnimations();
if (isShooting = true)
{
IsShooting = true;
}
else if (isShooting = false)
{
IsShooting = false;
}
}
void Shoot()
{
isShooting = true;
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.right * bulletForce, ForceMode2D.Impulse);
}
private void UpdateAnimations()
{
anim.SetBool("IsShooting", IsShooting);
}
}
but once i shot , the shooting animation will not stop running
Your IsShooting
variable is never set back to false, as your if statements are a bit flawed and will always keep the variable the same. But also it seems like what you’re trying to do would work better with an animation trigger rather than a boolean. Try changing the parameter in the animator to be a trigger instead, and in your Update method:
if (Input.GetButtonDown("Fire1"))
{
Shoot();
anim.SetTrigger("IsShooting");
}