//FollowPlayer(相机跟随主角)、food(控制food旋转)、Player(控制主角移动、碰撞器)
U3D源文件网盘链接
FollowPlayer,相机跟随主角
代码语言:javascript复制using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public GameObject player;
// Update is called once per frame
void Update()
{
Vector3 player_position = player.GetComponent<Transform>().position;
this.GetComponent<Transform>().position = new Vector3(player_position.x, player_position.y 13.14f, player_position.z - 13.98f);
}
}
控制food旋转
代码语言:javascript复制using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class food : MonoBehaviour
{
// Update is called once per frame
void Update()
{
this.transform.Rotate(Vector3.up); //此处transform小写!
}
}
Player,控制主角移动、碰撞器-is Trigger触发器
代码语言:javascript复制using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
int i = 0;
// Update is called once per frame
void Update()
{
float horizontal_move = Input.GetAxis("Horizontal"); //horizontal必须与unity名称一模一样!
float vertical_move = Input.GetAxis("Vertical"); //unity位置:Edit-Projectings-Input
this.GetComponent<Rigidbody>().AddForce(new Vector3(horizontal_move, 0, vertical_move) * 10);
}
void OnTriggerEnter(Collider other) //OnTriggerEnter是一个方法,且i不能定义在它里面,每次调用这个方法,i都会被初始化!
{
if (other.gameObject.name == "food")
{ //i不能定义在这里面!因为if每次都会将i初始化为0,永远都不可能“win”!
GameObject.Destroy(other.gameObject);
i ;
if (i == 10)
{
Debug.Log("U are win ! ");
}
}
}
}