【问题标题】:OnCollisionEnter2D making my object to disappear from game screenOnCollisionEnter2D 使我的对象从游戏屏幕上消失
【发布时间】:2014-12-12 21:18:11
【问题描述】:

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

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

}

【问题讨论】:

  • 我已经更新了enemyscript,没有错误。
  • 不,玩家是不同的,它是游戏的一部分,有一个玩家,炮塔也没有从层次结构中消失,它就在那里,我在场景中看到,它旋转并瞄准敌人,还有射击,因为敌人死了,子弹也看不见。,我肯定犯了一些愚蠢的错误。
  • 我在“游戏”中看不到它,在“场景”中它的出现意味着它的y轴设置为大于90度。

标签: c# unity3d


【解决方案1】:

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

【讨论】:

  • 子弹刚出来,并没有向敌人移动。,子弹有问题吗?
  • 有什么方法可以控制炮塔瞄准移动的速度吗?
  • 子弹的另一个问题。为什么 transform.LookAt 不起作用?
  • 但是我的子弹没有前进,我已经在炮塔类中实例化了代码。
猜你喜欢
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 2013-11-21
  • 1970-01-01
相关资源
最近更新 更多