comme le sujet l'indique , mon personnage ce prend des dégâts lors qu il attaque un ennemie
c'est un jeu de plateforme en 2D
mon personnage a une épée (Tag avec swordpl) , je lui ai créé un point d attaque à gauche et à droite pour faciliter la gestion de la rotation du personnage
mais lorsque j'attaque avec mon épée le collider box de l'ennemie pense que c'est mon personnage (Tag player) qui touche et non pas (swordpl)
donc je tue mon ennemie mais cela me crée un dégat a mon personnage
attackpoint et attackpoint2 sont les box collider des points d attaque gauche et droite
J' ai oublié de vous dire que les point d attaques sont les enfants de mon personnage
voici le code
playermove
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float MoveSpeed;
public float jumpforce;
public Rigidbody2D rb;
public SpriteRenderer sr;
public Animator Anim;
public GameObject pausepanel;
public GameObject attackpoint;
public GameObject attackpoint2;
private bool ispause = false;
private Vector3 velocity = Vector3.zero;
private bool isjump = false;
private bool isright = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float horizontalMovement = Input.GetAxis("Horizontal") * MoveSpeed * Time.deltaTime;
Vector3 targetvelocity = new Vector2(horizontalMovement, rb.velocity.y);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetvelocity, ref velocity, .05f);
float playervelo = Mathf.Abs(rb.velocity.x);
Anim.SetFloat("Run", playervelo);
flip(rb.velocity.x);
if (Input.GetButtonDown("Jump") && isjump)
{
jump();
}
else if (Input.GetButtonDown("Fire1"))
{
attack();
}
else if (Input.GetButtonDown("Cancel"))
{
if (ispause)
{
Time.timeScale = 1;
pausepanel.SetActive(false);
ispause = false;
}
else
{
Time.timeScale = 0;
pausepanel.SetActive(true);
ispause = true;
}
}
}
void jump()
{
rb.AddForce(new Vector2(0, jumpforce));
}
void flip(float _velocity)
{
if (_velocity > 0.1f)
{
sr.flipX = true;
//transform.Rotate(0f, 180, 0f);
isright = true;
}
else if (_velocity < -0.1f)
{
sr.flipX = false;
isright = false;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag("Ground"))
{
isjump = true;
}
}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.CompareTag("Ground"))
{
isjump = false;
}
}
public void attack()
{
if (isright)
{
attackpoint.SetActive(true);
Anim.SetTrigger("Attack");
StartCoroutine(attackdelay());
}
else
{
attackpoint2.SetActive(true);
Anim.SetTrigger("Attack");
StartCoroutine(attackdelay());
}
}
IEnumerator attackdelay()
{
yield return new WaitForSeconds(5f);
attackpoint.SetActive(false);
attackpoint2.SetActive(false);
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ennemymove : MonoBehaviour
{
public float speed;
public Transform[] waypoint;
public SpriteRenderer sr;
private Transform target;
private int destPoint;
public int damageoncolis;
void Start()
{
target = waypoint[0];
}
// Update is called once per frame
void Update()
{
Vector3 dir = target.position - transform.position;
transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
if (Vector3.Distance(transform.position, target.position) < 0.3f)
{
destPoint = (destPoint + 1) % waypoint.Length;
target = waypoint[destPoint];
sr.flipX = !sr.flipX;
}
}
private void OnCollisionEnter2D(Collision2D colision)
{
if (colision.transform.CompareTag("Player"))
{
Debug.Log("l ennemy ma touché");
PlayerHealth playerhealth = colision.transform.GetComponent<PlayerHealth>();
playerhealth.TakeDamage(damageoncolis);
}
if (colision.transform.CompareTag("swordpl"))
{
Debug.Log("l ennemy a toucher lepee");
Destroy(transform.parent.gameObject);
}
}
}