[英]Unity3d not liking collision
using UnityEngine;
using System.Collections;
public class chicken_for : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
if (collision.gameObject.Quad == collisionObject){
Application.LoadLevel("SciFi Level");
}
}
}
I am attempting to when A person touches a quad, he goes to this sci-fi fortress. 我试图当一个人碰到四边形时,他去了这个科幻堡垒。 It however says the name 'collision' does not exist in the current context.
但是,它说“冲突”这个名称在当前上下文中不存在。
You're referencing a variable ( collision
) that doesn't exist. 您引用的变量不存在(
collision
)。 You've got a variable collisionObject
that doesn't exist, too. 您也有一个不存在的变量
collisionObject
。
collision
is typically the name of the argument to the OnCollisionEnter
method. collision
通常是OnCollisionEnter
方法的参数名称。 Your code should probably be inside this method instead of FixedUpdate
. 您的代码可能应该在此方法内,而不是
FixedUpdate
。 I'm guessing that you've copied code from a tutorial somewhere but put it in the wrong method. 我猜您是从某个地方的教程中复制了代码,但是将其放在错误的方法中。
collisionObject
on the other hand is harder to guess, but I expect that if your script is intended to be a component on the player object, then collisionObject
should be your quad; collisionObject
,另一方面是难以猜测,但我相信,如果你的脚本旨在为玩家对象上的组件,然后collisionObject
应该是你的四核; if the script is on the quad then collisionObject
should be the player. 如果脚本位于四边形上,则
collisionObject
应该是播放器。
Either way, you need to declare that variable - probably as a public field so that you can populate it from the inspector. 无论哪种方式,您都需要声明该变量-可能是一个公共字段,以便可以从检查器中填充它。
Add a new function and make sure the player is tagged as player in the inspector. 添加新功能,并确保在检查器中将播放器标记为播放器。 You also need to make sure that there is a collider on the quad that the player touches and a rigidbody component on the player.
您还需要确保播放器接触的四边形上有一个碰撞器,并且播放器上有一个刚体组件。
using UnityEngine;
using System.Collections;
public class chicken_for : MonoBehaviour {
//This function will handle the collision on your object
void OnCollisionEnter (Collider col){
if(col.gameobject.tag == "Player"){ //if colliding object if tagged player
Application.LoadLevel("SciFi Level"); //load the sci-fi level
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
This should get you roughly where you want to be! 这样可以大致了解您想去的地方!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.