Hello! I'm finishing a melee system and I'm having some problem making the arrows take damage from the player when they hit it. Right Now I have one character that attacks properly with a 2d sword and this piece of script
void Attack(){
float distance = Vector2.Distance(Target.transform.position, transform.position);
Vector2 dis = (Target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dis, transform.forward);
if(distance < 0.45){
PlayerHealth PH = (PlayerHealth)Target.GetComponent("PlayerHealth");
PH.Attacked();
if (PH.curHealth == 0f)
Target = null;
}
}
And I'm trying to do something similar with this Bow And Arrow Script
using UnityEngine;
using System.Collections;
public class BowandArrow : MonoBehaviour {
private Animator anim;
bool FacingRight = true;
public Rigidbody2D arrow;
public float arrowforce = 10f;
public Vector3 Rotation;
public GameObject Target;
public float Depth = 0.30f;
private PlayerController playercontrll;
// Use this for initialization
void Start () {
anim = transform.root.gameObject.GetComponent();
playercontrll = transform.root.GetComponent();
}
void Update(){
if(Input.GetButtonDown("Attack")){
anim.SetTrigger("Shoot");
if(playercontrll.FacingRight){
Rigidbody2D arrowInstance = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
arrowInstance.velocity = new Vector2(arrowforce, 0);
}
else
{
Rigidbody2D arrowInstance = Instantiate(arrow,transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
arrowInstance.velocity = new Vector2(-arrowforce, 0);
}
}
}
void OnCollisionEnter2D(Collision2D coll){
if(coll.gameObject.tag == "Player 2"){
Player2Health PH2 = (Player2Health)Target.GetComponent("Player2Health");
PH2.Attacked2();
if (PH2.curHealth2 == 0f)
Target = null;
}
}
}
Everything works fine but It just doesn't damage the other player. It'd be great if you guys could give me some help
↧