自己做的一个超级简单的小游戏

    xiaoxiao2021-04-16  137

    自己做了一个简单的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>(); //获取player的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;//normalized :将向量长度归一 transform.position = movement + transform.position; playerRigidbody.MovePosition(transform.position); } /* 手机中重力感应控制小球行动(不知道能不能实现) void Update() { Vector3 dir = Vector3.zero; dir.x = -Input.acceleration.y; dir.z = Input.acceleration.x; if (dir.sqrMagnitude > 1) dir.Normalize(); dir *= Time.deltaTime; transform.Translate(dir * speed); } */ }

    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; //要在unity编辑器中给enemy设置NavMeshAgent void Awake() { player = GameObject.FindGameObjectWithTag("Player").transform; nav = GetComponent<UnityEngine.AI.NavMeshAgent>(); } // Use this for initialization void Start () { } // Update is called once per frame 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; // Use this for initialization void Awake() { text = GameObject.Find("Canvas/Text").GetComponent<Text>(); playermove = player.GetComponent<playermove>(); } // Update is called once per frame 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;//敌人刷新点 // Use this for initialization void Start () { InvokeRepeating("Spawn",spawntime,1f); //InvokeRepeating("函数名称",调用时间,调用间隔) //InvokeRepeating函数用来重复调用某一函数 } 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; //导入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

    最新回复(0)