how do i refer to the animation in the shooting script?
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);
}
}
Answer by GheSborooo · Dec 04, 2020 at 02:07 PM
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
Answer by vinvinn · Dec 04, 2020 at 09:48 PM
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");
}
Your answer
Follow this Question
Related Questions
Shooting animation and bullet generation issue 0 Answers
Help! Create interactive button with an image + c# Script 0 Answers
How can I stop my character from walking during the attack animation? 1 Answer
2D Animation Feet Pivot Point 0 Answers
How do I adjust the direction of my game object in the direction my player turns? 0 Answers