非常奇怪的问题,我甚至不确定为什么或如何发生这种情况,但是我有两个实际碰撞的游戏对象,因为我有一个事件发生的时候(爆炸),但物体实际上并没有再碰到另一个。突然间,当他们互相撞击时,他们无法互相阻止。如果这听起来很奇怪,我很抱歉,我几乎不知道如何描述我的问题。我会发布代码,不确定这是否会产生任何影响,但是,请问我任何你可能会“哦,我想这可能是为什么,你的设置是什么......”等等。他们曾经打过彼此,但现在他们只是穿过,好像他们没有任何关系,但他们碰撞,因为它引发爆炸。 (它不会破坏它,所以不,这不是因为游戏对象被破坏了)。
AIScript
using UnityEngine;
using System.Collections;
public class AIScript : MonoBehaviour {
private PlayerScript GO;
public Transform target;
public Transform myTransform2;
public GameObject Comp;
public GameObject splosion;
public int maxDistance;
public int rotationSpeed;
public int moveSpeed;
// Use this for initialization
void Start () {
GO = GameObject.Find("Player").GetComponent<PlayerScript>();
target = GO.transform;
maxDistance = 2;
}
void Awake() {
myTransform2 = transform;
}
// Update is called once per frame
void Update () {
if (GO.colorRed)
{
Debug.DrawLine (target.position, myTransform2.position, Color.red);
}
else
{
Debug.DrawLine (target.position, myTransform2.position, Color.blue);
}
//handles rotation of AI
myTransform2.rotation = Quaternion.Slerp (myTransform2.rotation, Quaternion.LookRotation(target.position - myTransform2.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform2.position) > maxDistance && GO.stopAI)
{
//Move towards target
myTransform2.position += myTransform2.forward * moveSpeed * Time.deltaTime;
}
}
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Player")
{
Instantiate (splosion, transform.position, transform.rotation);
}
}
}
PlayerScript
public class PlayerScript : MonoBehaviour {
// globals
private AIScript GO;
public string moveL;
public string moveR;
public string moveU;
public string moveD;
//
public float playerSpeed;
//
public bool colorRed;
public bool stopAI;
//
public GameObject splosion;
// Use this for initialization
void Start () {
GO = GameObject.Find("Computer").GetComponent<AIScript>();
}
// Update is called once per frame
void Update () {
stopAI = false;
if(Input.GetKey (moveL))
{
colorRed = true;
stopAI = true;
GO.Comp.renderer.material.color = Color.green;
SetTransformX((transform.position.x) - playerSpeed);
}
if(Input.GetKey (moveR))
{
colorRed = true;
stopAI = true;
GO.Comp.renderer.material.color = Color.red;
SetTransformX((transform.position.x) + playerSpeed);
}
if(Input.GetKey (moveU))
{
colorRed = false;
stopAI = true;
GO.Comp.renderer.material.color = Color.blue;
SetTransformY((transform.position.y) + playerSpeed);
}
if(Input.GetKey (moveD))
{
colorRed = false;
stopAI = true;
GO.Comp.renderer.material.color = Color.clear;
SetTransformY((transform.position.y) - playerSpeed);
}
else
{
rigidbody.angularVelocity = Vector3.zero;
}
}
void SetTransformX(float n)
{
transform.position = new Vector3(n, transform.position.y, transform.position.z);
}
void SetTransformY(float n)
{
transform.position = new Vector3(transform.position.x, n, transform.position.z);
}
}
答案 0 :(得分:1)
需要将OnTriggerEnter更改为OnCollisionEnter,然后使用Collision更改Collider。