Group Group Group Group Group Group Group Group Group

[Help] How to write 【knockback Enemy】

#1

Recently I was learning how to let the player attack the enemy ->knockback and make his Rotation.z appear to fall. . .
I am looking for some information but I can’t think of it, it makes me a bit lost.
I can only make him
The position changes to a new position when attacked, and the effect of being pushed back cannot be exhibited.
How can I explore?

#2

Hi @tony23035688, if you’re having a specific issue with changing positions during an attack could you possibly share some code with us to have a better understanding?

Best,
Gina

#3

Ok, I am trying to use this to present the player [attack]-> Enemy
Enemy Knockback (x, y) rendered by [knockback] distance and Repel fly
I am trying this Code

enemy

public IEnumerator Knockback(float knockDur,float knockbackPwr,Vector3 knockbackDir)
{

    float timer = 0;
    while ( knockDur>timer  )
    {
        timer += Time.deltaTime;
        //body.velocity = new Vector2(0, 0);
        //body.velocity = new Vector2(body.velocity.x, 0);
        //moveDirection = body.transform.position - target.transform.position;
        body.AddForce(new Vector3(knockbackDir.x -100, knockbackDir.y+knockbackPwr,transform.position.z), ForceMode2D.Impulse);
        //body.AddForce(moveDirection.normalized * 500f, ForceMode2D.Impulse);
    }
    yield return 0;
    //StopCoroutine("Knockback");
}

I also doubt if it is my mobile problem.
void FixedUpdate()
{
float movSpeed = Time.fixedDeltaTime * speed * speedRate * Mathf.Sign(targetPos.x - transform.position.x);
Vector3 vel = body.velocity;
vel.x = (moving && !Game.pause && enableMove) ? movSpeed : 0;
body.velocity = vel;

    // Direction determination
    if (body.velocity.x != 0)
    {
        Vector3 angle = transform.eulerAngles;
        angle.y = body.velocity.x > 0 ? 0 : 180;
        transform.eulerAngles = angle;
    }
    // Stop at the target point
    if (ReachGoal())
    {
        // Call bound event
        if (OnReach != null)
        {
            OnReach();
            OnReach = null;
        }
        StopMove();
    }

}

PlayerAttack

public class PlayerAttack : MonoBehaviour
{
internal AllBossAiSet Boss ;
//internal Player Player;
//public Vector3 moveDirection;

private float timeBtwAttack;
public float startTimeBtwAttack;


public Transform attackPos;
public LayerMask whatIsEnemies;
public float attackRange;


private void Awake()
{
    Boss = GameObject.FindGameObjectWithTag("Enemy").GetComponent<AllBossAiSet>();
   // Player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
//void Start() {}

void Update()
{
    if (timeBtwAttack <= 0)
    {
        if (Input.GetKey(KeyCode.S))
        {
            Collider2D[] enemiesToKnockback = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
            for (int i = 0; i < enemiesToKnockback.Length; i++)
            {
                enemiesToKnockback[i].GetComponent<AllBossAiSet>().TakeDamage(1);
                StartCoroutine(Boss.Knockback(0.1f,5f,Boss.transform.position));
                //moveDirection = Player.playerRigidbody2D.transform.position - Player.transform.position;
                //Boss.body.AddForce(moveDirection.normalized * -500f);
               
            }
            Debug.Log("Click!");           
        }
        timeBtwAttack = startTimeBtwAttack; 
    }
    else
    {
        timeBtwAttack -= Time.deltaTime;
    }
}
   
private void OnDrawGizmosSelected()
{
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere(attackPos.position, attackRange);
}

}

closed #4

This topic was automatically closed after 166 days. New replies are no longer allowed.