2014-12-11 2 views
0

나는 게임 오브젝트 인 포탑을 가지고 있으며, 충돌을 감지하기 위해 상자 콜리더를 만들었습니다. 적이 적을 상자에 넣었을 때, 그것을 목표로합니다. 그것을 쏘지 만, 어떤 이유에서 포탑이 적의 충돌 상자에 들어간 후에 포탑이 사라지고, 또한 사격이 보이지 않습니다. 내가 잘못했는지, 어떤 도움이 되었습니까!OnCollisionEnter2D 게임 화면에서 내 물건을 사라지게하기

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 

public class TurretScript : MonoBehaviour { 

public float shotInterval = 0.2f; // interval between shots 
public Rigidbody2D bulletPrefab; // drag the bullet prefab here 

private float shootTime = 0.0f; 
private List<Transform> targets; 
private Transform selectedTarget; 
private Transform myTransform; 
private Transform bulletSpawn; 

void Start(){ 
    targets = new List<Transform>(); 
    selectedTarget = null; 
    myTransform = transform; 
    bulletSpawn = transform.Find ("bulletSpawn"); // only works if bulletSpawn is a turret child! 
} 

void OnTriggerEnter2D(Collider2D other){ 
    if (other.tag == "enemy"){ // only enemies are added to the target list! 
     targets.Add(other.transform); 
    } 
} 

void OnTriggerExit2D(Collider2D other){ 
    if (other.tag == "enemy"){ 
     targets.Remove(other.transform); 
    } 
} 

void TargetEnemy(){ 
    if (selectedTarget == null){ // if target destroyed or not selected yet... 
     SortTargetsByDistance(); // select the closest one 
     if (targets.Count > 0) selectedTarget = targets[0]; 
     Debug.Log ("selected target is"+selectedTarget); 
    } 
} 

void SortTargetsByDistance(){ 
    targets.Sort(delegate(Transform t1, Transform t2){ 
     return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); 
    }); 
} 

void Update(){ 
    TargetEnemy(); // update the selected target and look at it 
    if (selectedTarget) 
    { 
     // if there's any target in the range... 
     transform.LookAt(selectedTarget); // aim at it 
     if (Time.time >= shootTime){// if it's time to shoot... 
      Rigidbody2D bullet = (Rigidbody2D)Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation); 
      bullet.AddForce(transform.forward*5); // shoot in the target direction 
      shootTime = Time.time + shotInterval; // set time for next shot 
     } 
    } 
} 
    } 

여기에 적 코드

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 

public class EnemyScript : MonoBehaviour { 

public Transform target; 
public float speed = 2f; 

public int Health; 

public float GetHealth() 
{ 
    return Health; 
} 


    void Update() 
    { 
    transform.position = Vector2.MoveTowards(transform.position, target.position, speed  * Time.deltaTime);                  
    } 

    void TakeDamage(int damage){ 
    Health -= damage; 
    if (Health <= 0) 
     Destroy(gameObject); 
} 


void OnTriggerEnter2D(Collider2D otherCollider) 
{ 
    PlayerControl shot = otherCollider.gameObject.GetComponent<PlayerControl>(); 
    if (shot != null) 
    { 
     SpecialEffectsHelper.Instance.Explosion(transform.position); 
     Destroy(shot.gameObject); 
    } 
} 

} 
+0

이 오류가없는 :

이 대신처럼 돌려보십시오. – Safwan

+0

아니요, 플레이어는 플레이어가있는 게임의 일부와 다르며 포탑은 협죽도에서 사라지지 않습니다. 또한 거기에는 장면을 보았습니다. 회전하고 목표물을 향한 적과 사격을하기 때문에 적군이 죽고 총알도 보이지 않습니다. 분명히 바보 같은 실수를 저질렀습니다. – Safwan

+0

나는 "게임"에서 그것을 볼 수 없으며, "장면"에서 그 의미를 보여주고, y 축은 90도 이상으로 설정됩니다. – Safwan

답변

0

transform.LookAt 그것을 볼 수는없는하게 당신의 이동을 회전이다.

나는 enemyscript를 업데이트
Vector3 dir = selectedTarget.position - transform.position; 
float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg; 
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); 
+0

총알이 방금 나오고 적을 향해 움직이지 않습니다. 총알에 문제가 있습니까? – Safwan

+0

포탑 이동 속도를 제어 할 수있는 방법이 있습니까? – Safwan

+0

총알에 대한 또 다른 문제점. 왜 transform.LookAt가 작동하지 않았습니까? – Safwan

관련 문제