【2023unity游戏制作-mango的冒险】-2.开始画面API制作

简介: 【2023unity游戏制作-mango的冒险】-2.开始画面API制作

在这里插入图片描述


👨‍💻个人主页@元宇宙-秩沅

**hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!**

本文由 秩沅 原创

收录于专栏游戏制作


⭐mango的冒险-开始画面制作⭐
-


@[TOC]


👨‍💻前言


🎶版本: 为 Ltp 2021年版本的unity
🎶类型: 简单2D类冒险游戏
🎶目的: 熟悉掌握基本unityAPI
🎶视频教程:【2023小白狂飙unity2D冒险类游戏制作【mango的冒险】】


👨‍💻mango的定向移动和动画切换


  • 利用MoveToword函数和延时调用方法。
    在这里插入图片描述
    ❤️关键源码:
    void FixedUpdate()
      {
         
         
          time = Mathf.MoveTowards(time, 0, 0.1f);//倒计时,相当于起到一个延时调用的作用
          if (time == 0)
          {
         
         
              gameObject.transform.position = Vector2.MoveTowards(gameObject.transform.position, point.transform.position, 0.1f);
          }
      }
    

❤️源码:

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

public class Movetowords : MonoBehaviour
{
   
   
    private GameObject point;
    private Animator  mangoAni;
    private float time = 5 ;
    void Start()
    {
   
   
        point = GameObject.Find("add");
        mangoAni = GetComponent<Animator>();
    }
    private void Update()
    {
   
   
        Debug.Log (gameObject.transform.position+"和"+point.transform.position);
        if (gameObject.transform.position.x == point.transform.position.x)
        {
   
   
            mangoAni.CrossFade("LookUp", 0f);
        }
    }
    void FixedUpdate()
    {
   
   
        time = Mathf.MoveTowards(time, 0, 0.1f);//倒计时
        if (time == 0)
        {
   
   
            gameObject.transform.position = Vector2.MoveTowards(gameObject.transform.position, point.transform.position, 0.1f);
        }

    }
}

👨‍💻吟唱召唤法球


  • 第一步:首先固定六个点,前三个作为法球的生成位置(开始点),后三个作为法球的终端(下落点)
  • 第二步:用数组存储所有点的位置
  • 第三步:创建法球
  • 第四步:延时调用该方法

关键代码

 //1.法球生成
            AllIea[i] = Instantiate<GameObject>(profab, Allpoint[i].position, Quaternion.identity);
            //2.实现法球移动效果
            IeaMove mation = AllIea[i].GetComponent<IeaMove>();
            mation.Pball  = Allpoint[i + 4] ; 
//3.法球渲染层级设置
            {
   
   
                profab.GetComponent<SpriteRenderer>().sortingOrder = 3;
            }

在这里插入图片描述
GIF效果:
在这里插入图片描述
❤️源码:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.UIElements;

//----------------------
//--作用:mango的移动和法球的生成
//----------------------

public class Movetowords : MonoBehaviour
{
   
   
    private GameObject point, profab;
    private Animator mangoAni;
    private Transform[] Allpoint = new Transform[8];
    private GameObject[] AllIea = new GameObject[4];
    private float time = 5;

    void Start()
    {
   
   
        point = GameObject.Find("add");
        profab = Resources.Load<GameObject>(@"prefab1/iea");
        mangoAni = GetComponent<Animator>();
        for (int i = 0; i < Allpoint .Length ; i++)
        {
   
   
            Allpoint[i] = GameObject.Find("Allpoint").transform.GetChild(i);

        }
        Invoke("Creatball", 5);
    }
    private void Update()
    {
   
   
        //当位置到达后,动画转为吟唱动画
        if (gameObject.transform.position.x == point.transform.position.x)
        {
   
   
            mangoAni.CrossFade("LookUp", 0f); //
        }
    }
    void FixedUpdate()
    {
   
   
        time = Mathf.MoveTowards(time, 0, 0.1f);//倒计时,相当于起到一个延时调用的作用
        if (time == 0)
        {
   
   
            gameObject.transform.position = Vector2.MoveTowards(gameObject.transform.position, point.transform.position, 0.1f);
        }
    }

    private void Creatball()  //创建法球
    {
   
   
        for (int i = 0; i < AllIea.Length ; i++)
        {
   
   
           if(i == 3) //3.法球渲染层级设置
            {
   
   
                profab.GetComponent<SpriteRenderer>().sortingOrder = 3;
            }
           //1.法球生成
            AllIea[i] = Instantiate<GameObject>(profab, Allpoint[i].position, Quaternion.identity);
            //2.实现法球移动效果
            IeaMove mation = AllIea[i].GetComponent<IeaMove>();
            mation.Pball  = Allpoint[i + 4] ; 
         }
    }

}

💕法球上的脚本代码

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

//----------------------
//--作用:法球的定向移动
//----------------------

public class IeaMove : MonoBehaviour
{
   
   
    public Transform Pball;

    void FixedUpdate()
    {
   
   
        transform.position = Vector2 .MoveTowards(transform.position , Pball.position,0.01f );
    }
}

👨‍💻添加拖尾粒子特效


GIF效果:
在这里插入图片描述

  • 在法球中添加Trail Render组件
  • 参数更改为如图所示:
    在这里插入图片描述

每天一更,持续更新!


⭐相关文章⭐
-

【unity游戏制作-mango的冒险】-场景搭建

“狂飙”游戏制作---游戏分类图鉴(网易游学)

本站最全-unity常用API大全(万字详解),不信你不收藏


你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!

目录
相关文章
|
11月前
|
机器学习/深度学习 编解码 数据可视化
【unity本站最全系列】unity常用API大全一篇文章足以(万字详解)不信你不收藏
【unity本站最全系列】unity常用API大全一篇文章足以(万字详解)不信你不收藏
739 1
|
5月前
|
API C# 图形学
【Unity 3D】常见API的讲解以及在C#脚本中的执行(附源码)
【Unity 3D】常见API的讲解以及在C#脚本中的执行(附源码)
70 1
|
11月前
|
API 图形学
【2023unity游戏制作-mango的冒险】-前六章API,细节,BUG总结小结
【2023unity游戏制作-mango的冒险】-前六章API,细节,BUG总结小结
98 0
|
11月前
|
API 图形学
【2023unity游戏制作-mango的冒险】-1.场景搭建
【2023unity游戏制作-mango的冒险】-1.场景搭建
75 0
|
11月前
|
编译器 API 图形学
unity常用API基础知识
常用API基础知识
152 0
|
JSON API 图形学
Unity 接入高德开放API - 天气查询
Unity 接入高德开放API - 天气查询
420 1
Unity 接入高德开放API - 天气查询
|
API 图形学
Unity中GameObject API解析
如何在脚本中获取自身所在的GameObject? 我们知道脚本必须挂在一个GameObject上面才能执行,那么如何知道当前脚本所挂载的GameObject是哪一个呢? 这就需要用到gameObject属性了。
8720 0
|
API C#
Unity常用旋转API
Unity中的旋转通常可以用Transform 直接控制和 rotation 控制两种方式。 一、Transform控制(工程中的scene1) 1.1 Transform.Rotate 旋转某个角度 函数定义 [csharp] view plain copy public void Rotate...
11652 0
|
API C# 图形学
Unity SceneManager场景管理Chinar详解API
本文提供全流程,中文翻译。 Unity SceneManager场景管理Chinar详解API 助力快速理解 Unity 场景管理的 API 诸多用法 Unity 场景API中文详解 我们在游戏开发中,经常用到关卡切换、场景切换、加载场景等诸多功能性操作 Unity 为我们提供了场景管理类.
6639 0