您的位置:首页 > 移动开发 > Unity3D

在Unity世界里遇到的第一个错误!

2015-10-22 17:44 579 查看
今天看了一个视频是介绍碰撞检测的,然后我做了一个实验来测试碰撞检测。



首先是create了4个cube,分别命名为C1,C2,C3,C4,然后写了一个脚本名为Move.cs来利用键盘wsad键控制C4的上下左右移动,

代码如下:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {
GameObject go;
// Use this for initialization
void Start () {
go = GameObject.Find("C4");
go.renderer.material.color = Color.red;
}

// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W))
{
go.transform.Translate(0, 0, 5 * Time.deltaTime, Space.Self);
}
if(Input.GetKey(KeyCode.S))
{
go.transform.Translate(0, 0, -5 * Time.deltaTime, Space.Self);
}
if (Input.GetKey(KeyCode.A))
{
go.transform.Translate(-5 * Time.deltaTime, 0, 0, Space.Self);
}
if (Input.GetKey(KeyCode.D))
{
go.transform.Translate(5 * Time.deltaTime, 0, 0, Space.Self);
}
}
}


然后又写了一个脚本让被C4碰撞的物体变色:

using UnityEngine;
using System.Collections;

public class MoveCollision : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
void OnCollisionEnter(Collision co)
{
co.gameObject.renderer.material.color = Color.blue;
}
}
在这里重点说一下:我之前的代码名字不是叫MoveCollision.cs ,而是叫Collision.cs,然后就出现了如下错误:

Script error: OnCollisionEnter

This message parameter has to be of type: Collision

The message will be ignored.

这是因为Collision 这个名字与Unity的内置名称相冲突,所以不能以此命名,应改为MoveCollision;

这样,程序完美输出!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: