OnCollisionEnter2D让我的对象从游戏屏幕上消失

时间:2014-12-11 08:42:08

标签: c# unity3d

我有一个炮塔,这是一个游戏对象,我已经在它周围建立了一个箱子对撞机来检测碰撞,如果我的敌人进入了turet的boxcollider,它瞄准它并射击它,但由于某种原因,在敌人进入碰撞盒后,炮塔就会消失。而且射击也是不可见的。我在哪里错了,任何帮助,都不是啊!

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); 
    }
}

}

1 个答案:

答案 0 :(得分:0)

transform.LookAt正在旋转你的go,这使你无法看到它。

尝试像这样旋转它:

Vector3 dir = selectedTarget.position - transform.position;
float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);