- Home /
Question by
Resiri · Apr 29, 2021 at 10:48 AM ·
c#2d game2d-platformer
Move Player forward then back after seconds
Hi, I'm writing a 2D combat script and I'd like to make it so that when the attack button is pressed, the player moves forward for a lunge then back after a few seconds
This is the code
private Rigidbody2D rb;
private float timeBetweenAttack;
public float startTimeBetweenAttack;
public Transform attackPos;
public LayerMask whatIsEnemies;
public float attackRange;
public int damage;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (timeBetweenAttack <= 0)
{
if (Input.GetKey(KeyCode.F))
{
Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
for (int i = 0; i < enemiesToDamage.Length; i++)
{
enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
}
}
timeBetweenAttack = startTimeBetweenAttack;
}
else
{
timeBetweenAttack -= Time.deltaTime;
}
}
New to Unity and trying to learn, thank you for your time
Comment
Answer by rawa800x · Apr 29, 2021 at 04:45 PM
Just use the Getcomponent().Addforce(transform.forward * 100/-100) Then put that into an IE numerator then wait for seconds, then type the getcomponent with negative.