unity投篮小游戏,原创,请勿转载。

您所在的位置:网站首页 unity怎么做vr游戏 unity投篮小游戏,原创,请勿转载。

unity投篮小游戏,原创,请勿转载。

2023-09-16 18:03| 来源: 网络整理| 查看: 265

新手来看看,简单小游戏,首先先看看运行界面 ,玩法是点击鼠标左键球会跳动,进篮筐后篮筐消失,然后在另一边生成,生成的高度是随机的。   

一,先看看场景。

背景,篮板,上下左右都设置边界,球碰撞边界则此球出界。

1篮板设置碰撞体,由盒子型,胶囊型组成。

这里我设置了四个胶囊碰撞体在篮筐用于检测。

篮板,球杆设置盒子型碰撞体。

 

2.球的设置

刚体这些设置可以自己调,限制球在x轴的移动,和在y,z的转向,把写好的脚本拉过来,刚体之类的填补。

我为球添加了子物体,用于添加粒子特效,这只是简单特效,具体炫酷特效可以自己去研究

 

特效很简单,自己可以琢磨做更炫酷的特效。

二,接下来看源码。

ballMovement.cs源码。

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ballMovement : MonoBehaviour {     public float speedY = 10;// y轴速度     public Rigidbody rb;     public float speedZ = 5; //Z轴速度     public int i = 0;   //设置变量检测球框位置     public float mark = 0; //分数     public int health = 5; //生命值              void Start()     {         rb = GetComponent(); //获取刚体组件        

    }          void Update()     {         //如果按下鼠标左键或者空格键,则给球一个力为Vector3(0, speedY, speedZ);         if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))         {             rb.velocity = new Vector3(0, speedY, speedZ);          

        }        //更新分数                GameObject.Find("Canvas").GetComponentInChildren().text = "分数:" + mark + "";     }         void OnTriggerEnter(Collider other)      {         //如果进球,且篮板在左边,首先加分,改变球速度z方向,然后移动球框到右边,a是移动位置y轴的随机值,同时也要把球框调转         //debug用于调试         //后设置i = 1,改变位置         if (other.gameObject.name == "goal" && i == 0 )         {             mark -= (int)rb.velocity.y;             speedZ = -speedZ;             float a = Random.Range(-13f, 1.3f);                         GameObject.Find("BallFrame").transform.position = new Vector3(0, a, 15f);             GameObject.Find("BallFrame").transform.Rotate(0, 180, 0);             i = 1;             Debug.Log(rb.velocity);         }         //如果进球,且篮板在右边,首先加分,改变球速度z方向,然后移动球框到左边,a是移动位置y轴的随机值,同时也要把球框调转         //debug用于调试         //后设置i = 0,改变位置         else if (other.gameObject.name == "goal"&& i==1)         {             speedZ = -speedZ;             float a = Random.Range(-13f, 1.3f);             GameObject.Find("BallFrame").transform.position = new Vector3(0, a, 0.25f);             GameObject.Find("BallFrame").transform.Rotate(0, 180, 0);             i = 0;             mark -= (int)rb.velocity.y;             Debug.Log(rb.velocity.y);         }         //如果球出界,触碰边界,则生命值就减一。         //球回到原位置         //当生命值小于或者等于0时,游戏结束,消除游戏体——ball              if (other.gameObject.name == "end"|| other.gameObject.name == "floor")         {             print("此球已经出界了");             health--;             GameObject.Find("ball").transform.position = new Vector3(0, 5, 13);             if(health



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3