学习【见缝插针】小游戏案例使用到的知识点

11 篇文章 0 订阅

旋转:

transform.Rotate(0, 0, 1);
//每帧z轴加1
transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime));
//Rotate和Rotation 有区别

获取鼠标点击:

Input.GetMouseButton(0);
//左键0 中键1 右键2

移动:

//方法一:一次只能移动固定的值,见缝插针不适用
transform.Translate(1, 0, 0);
//方法二:
transform.position= Vector3.MoveTowards(gameObject.transform.position, Circle.transform.position, 10*Time.deltaTime);
//第一个函数是开始移动的位置
//第二个函数是移动的目标位置
//第三个函数是速度

找到游戏中的 物体:

//方法一:直接代码寻找
private Transform startPoint;
//Transform或者GameObject
startPoint = GameObject.Find("StartPoint").transform;
//方法二:直接到Unity拖动
public GameObject PinPrefab;
//方法二容易丢失,换版本等,能用代码就用代码

实例化对象:

GameObject.Instantiate(PinPrefab, insPoint.position, PinPrefab.transform.rotation);
//第一个函数是要实例化的物体
//第二个函数是要实例化的位置
//第三个函数是实例化物体的角度

判断两个物体之间的距离:

 if (Vector3.Distance(transform.position, startPoint.position) < 0.05)
 //如果两个物体之间的距离小于0.05就等于True
 //不能直接等于0,因为电脑会有一些误差

获取当前针,调用针物体脚本里的方法:

public Pin currentPin;//当前针 
currentPin= GameObject.Instantiate(PinPrefab, insPoint.position, PinPrefab.transform.rotation).GetComponent<Pin>();
//获取当前实例化的针
//并且可以
if (Input.GetMouseButtonDown(0))
        {
            currentPin.StartFly();//调用针物体里的StartFly方法
        }

设置结束点:

private Vector3 endPoint;//结束位置
endPoint = circlePos.position;
endPoint.y -= 17;

将针设置为圆的子物体(将圆设置为针的父物体)

transform.parent = circlePos;

关掉另一个物体的脚本:

Circle.GetComponent<Circle>().enabled = false;
//让圆身上的Circle脚本的Enable值为False

调用另一个对象身上的脚本里的方法:

GameObject.Find("GameManger").GetComponent<GameManger>().GameOver();
//调用GameManger对象身上的GameManger脚本里的GameOver方法

获取UI界面(实现分数显示)

using UnityEngine.UI;
//先引用命名空间
public Text scoreText;
//分数显示的文本
public int score=0;
//分数
scoreText.text = score.ToString();
//让文本显示分数

获取摄像机:

public Camera mainCamera;
//相机
mainCamera = Camera.main;
//获取主相机

协程?:

IEnumerator GameOverAnimation()
    {
        while (true)
        {

        }
    }

摄像机颜色渐变和摄像机缩放和延迟一秒重新加载场景:

IEnumerator GameOverAnimation()
    {
        while (true)
        {
            speed += Time.deltaTime;
            mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed);
            mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, 52, speed);
            if (Mathf.Abs(mainCamera.fieldOfView - 8) == 52)
            {
                break;
            }
            yield return 0;
        }
        //延迟调用
        yield return new WaitForSeconds(1);
        //延迟一秒
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        //加载场景
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值