初学Unity,第一个项目工程,如图所示,整个工程就是这么简单,一个场景,三个脚本文件(
Test.cs
是测试用的)。
场景:
demo01:场景的名称,用于加载游戏画面
Main Camera:主相机,用于展示游戏场景中的游戏物体(`GameObject` 本示例中,相机跟随`Player`)
Light:灯光,用于照亮场景
Ground:场景中的地板
Player:游戏猪脚(小圆球)
Food:旋转的Cube
资源目录:
Prefabs:预设文件夹,存放游戏预设,本工程主要用于存放复制属性相同多份的Food游戏物体
Scenes:场景文件夹,存放游戏场景
Scripts:脚本文件夹,存放c#脚本(用c#编写)
相机使用 FollowPlayer.cs
脚本
FollowPlayer代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour {
// 游戏猪脚对象
public GameObject player;
void Update () {
// 获取主角的 position
Vector3 player_position = player.GetComponent<Transform>().position;
// 设置相机的 position
this.GetComponent<Transform> ().position = new Vector3 (player_position.x, player_position.y + 8.3f, player_position.z - 8.3f);
}
}
Food食物使用 Food.cs
脚本
Food.cs
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Food : MonoBehaviour {
void Update () {
// 每秒旋转 60 度
this.transform.Rotate (Vector3.up);
}
}
猪脚使用 Player.cs
脚本
Player.cs
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
// 记录吃掉几个 食物(Food)
private int i = 0;
void Update () {
// Get the horizontal and vertical axis.
// 获取横向和纵向坐标轴
// By default they are mapped to the arrow keys.
// 默认情况下他们关联到方向键上
// The value is in the range -1 to 1
// 值的范围是在-1到1之间
float horizontal_move = Input.GetAxis ("Horizontal");
float vertical_move = Input.GetAxis ("Vertical");
// 给猪脚施加力
this.GetComponent<Rigidbody>().AddForce (new Vector3(horizontal_move, 0 , vertical_move)*10f);
}
// 当进入触发器
void OnTriggerEnter(Collider other){
Debug.Log (other.gameObject.name);
if (other.gameObject.name == "Food") {// 如果碰到的是 食物 进行销毁 食物
// 销毁 食物
GameObject.Destroy (other.gameObject);
i++;
Debug.Log ("Eat a food");
if (i==10) {
Debug.Log ("Game win!");
}
}
}
}
Input.GetAxis 获取轴<摘自Unity圣典>
static function GetAxis (axisName : string) : float
Description描述
Returns the value of the virtual axis identified by axisName.
根据坐标轴名称返回虚拟坐标系中的值。
The value will be in the range -1...1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.
使用控制器和键盘输入时此值范围在-1到1之间。如果坐标轴设置为鼠标运动增量,鼠标增量乘以坐标轴灵敏度的范围将不是-1到1 。
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour{
public float speed = 10.0F;
public float rotationSpeed = 100.0F;
void Update() {
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
}
}
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}