我希望检查3个对象(boxColliders)是否与其他Cubes发生冲突。如果碰撞为TRUE,则对象脚本必须通知Player.How如何在两个脚本之间移动?如果对象与立方体碰撞,则玩家无法移动到此方向。
我试过这样的事情,但脚本只检查了玩家。
void OnCollisionStay(Collision collisionInfo){
if(collisionInfo.gameObject.tag == "Finish")
{
Debug.Log ("collision");
}
}
这是完整的播放器代码
private Vector3 offset;
public GameObject player;
public GameObject center;
public GameObject right;
public GameObject left;
public int step=9;
public float speed =(float) 0.01;
bool input=true;
void Start () {
}
void Update () {
if(input==true)
{
if (Input.GetKey (KeyCode.RightArrow)) {
StartCoroutine ("moveRight");
input = false;
}
if (Input.GetKey (KeyCode.LeftArrow)) {
StartCoroutine ("moveLeft");
input = false;
}
}
}
IEnumerator moveLeft(){
for(int i=0;i<(90/step);i++){
player.transform.RotateAround (left.transform.position, Vector3.forward, step);
yield return new WaitForSeconds (speed);
}
center.transform.position = player.transform.position;
input = true;
}
IEnumerator moveRight(){
for(int i=0;i< (90/step);i++){
player.transform.RotateAround (right.transform.position, Vector3.back, step);
yield return new WaitForSeconds (speed);
}
center.transform.position = player.transform.position;
input = true;
}
void OnCollisionStay(Collision collisionInfo){
if(collisionInfo.gameObject.tag == "Finish")
{
Debug.Log ("collision");
}
}
答案 0 :(得分:0)
将碰撞器和碰撞事件放在要识别碰撞的所有游戏对象上。如果您只想访问其他对象的脚本,并且您不确定与您发生冲突的对象可以执行以下操作:
void OnCollisionStay(Collision collisionInfo)
{
var cubeScript = collisionInfo.gameObject.GetComponent<CubeScriptTypeHere>();
var playerScript = collisionInfo.gameObject.GetComponent<PlayerScriptTypeHere>();
if(cubeScript != null)
{
//This object hit a cube
//Do something with cubeScript
}
if(playerScript != null)
{
//This object hit a player
//Do something with playerScript
}
}
显然取代&#34; ScriptTypeHere&#34;用你命名的实际脚本。然后,您可以从碰撞事件中访问外部脚本的所有公共方法,属性等。