自己做了一个简单的uniy3d小游戏
方向键控制一个小球躲避别的小球,撞到指定的墙胜利,被别的球撞到失败。
初学者练手
Player
move
Enemy
moveIslose_TriggerSpawnCamera
follow_playerUI
Wall
IsWin
游戏图片
代码
player
player_move 控制小球移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermove : MonoBehaviour {
public float speed =
20f;
Vector3 movement;
Rigidbody playerRigidbody;
void Awake()
{
playerRigidbody=GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxisRaw(
"Horizontal");
float v = Input.GetAxisRaw(
"Vertical");
move(h,v);
}
void move(
float h,
float v)
{
movement.Set(h,
0f,v);
movement = movement.normalized * speed * Time.deltaTime;
transform.position = movement + transform.position;
playerRigidbody.MovePosition(transform.position);
}
}
Camera
Camerafollow 摄像机跟随小球
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camerafollow : MonoBehaviour {
public Transform target;
public float speed =
5f;
Vector3 offset;
void Awake()
{
offset = transform.position - target.position;
}
void FixedUpdate()
{
Vector3 targetcampos = target.position + offset;
transform.position = Vector3.Lerp(transform.position,targetcampos,speed*Time.deltaTime);
}
}
Enemy
Enemy_move
敌人移动(跟随小球)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemymove : MonoBehaviour {
Transform player;
UnityEngine.AI.NavMeshAgent nav;
void Awake()
{
player = GameObject.FindGameObjectWithTag(
"Player").transform;
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Start () {
}
void Update () {
nav.SetDestination(player.position);
}
}
Islose
如果player撞到enemy,显示lose,并且将playermove设置为false
Enemy的碰撞器上的IsTrigger打开
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Islose : MonoBehaviour {
public GameObject player;
playermove playermove;
Text text;
void Awake()
{
text = GameObject.Find(
"Canvas/Text").GetComponent<Text>();
playermove = player.GetComponent<playermove>();
}
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
text.text =
"LOSE!";
playermove.enabled =
false;
}
}
}
Spawnenemy
生成敌人
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spwanenemy : MonoBehaviour {
public GameObject enemy;
public float spawntime =
3f;
public Transform[] spawnpoints;
void Start () {
InvokeRepeating(
"Spawn",spawntime,
1f);
}
void Spawn()
{
int spawnPointIndex = Random.Range(
0, spawnpoints.Length);
Instantiate(enemy, spawnpoints[spawnPointIndex].position, spawnpoints[spawnPointIndex].rotation);
}
}
Wall
IsWin
小球撞到指定的墙上,显示Win
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Iswin : MonoBehaviour {
public GameObject player;
playermove playermove;
Text text;
void Awake()
{
text = GameObject.Find(
"Canvas/Text").GetComponent<Text>();
playermove = player.GetComponent<playermove>();
}
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
text.text =
"WIN!";
playermove.enabled =
false;
}
}
}
代码参考官方教程
新手练手,还请大拿多多指教
转载请注明原文地址: https://ju.6miu.com/read-672371.html