👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:unity常用API
⭐女神节专题篇⭐
文章目录
- ⭐女神节专题篇⭐
- 🎶前言
- 🎶(==A==)触发宝箱吃东西
- 🎶(==火==)触发宝藏逻辑实现
- 🎶(==土==)宝箱特效逻辑实现
- 🎶(==木==)法球上下的移动效果
- 🎶(==金==)炮管的旋转和发射
- 🎶(==电==)人物的移动和法球的生成
- 🎶(==水==)运动状态的跟随移动
- 🎶(==霾==)人物的移动
- ⭐相关文章⭐
🎶前言
🅰️ 让CSDN的浪漫弥漫女神节💕
🎶(A)触发宝箱吃东西
🎶(火)触发宝藏逻辑实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//----------------------
//--作用: 宝箱触发宝藏
//----------------------
public class showApple : MonoBehaviour
{
public Animator Colltor;
private GameObject apple;
void Start()
{
Colltor = GetComponent<Animator>();
}
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision) //设置触发器
{
if(collision.transform .tag =="Palyer")
{
Colltor.SetBool("swtch", true);
Invoke("CreatApple", 0.1f); //延时生成
}
}
private void CreatApple()
{
//实例化Apple
apple = Resources.Load<GameObject>(@"prefab1/apple");
Instantiate<GameObject>(apple, new Vector3(-5.59f, -1.36f, 0), Quaternion.identity);
}
}
🎶(土)宝箱特效逻辑实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//----------------------
//--作用: 宝箱特效
//----------------------
public class destory : MonoBehaviour
{
public SpriteRenderer SRender;
private bool Switch1;
private float i = 20;
private void Start()
{
SRender = GetComponent<SpriteRenderer>();
}
void Update()
{
if(Switch1 == true ) //进行恶魔果实的特效变换
{
Apple();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision .name =="Mango")
{
print("触发了");
Switch1 = true;
BallMove B_switch = collision.GetComponent<BallMove>(); //脚本调用获得打开法球冲散开关
B_switch.switchCrush = true;
print("恶魔果实效果开启");
}
/* }
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.name == "Mango")
{
Destroy(gameObject);
} */
}
private void Apple()
{
SRender.sprite = Resources.Load<Sprite>(@"prefab1/wow1"); //特效替换
SRender.sortingOrder = 1;
SRender.transform.localScale += new Vector3(1, 1, 0) * Time.deltaTime;
i = i - 0.1f;
SRender.color = new Color(255 ,0, 0,i)*Time.deltaTime;
if (i == 0) //当透明度alpha为0时销毁
{
Destroy(gameObject);
}
}
}
🎶(木)法球上下的移动效果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//----------------------
//--作用:法球的上下效果晃动
//----------------------
public class BallShape : MonoBehaviour
{
private float UpDown;//申明上下变化的数值
private float YPell;
private float endTime = 2;
private Vector2 Ball;
void Start()
{
YPell = transform.position.y; //将mango辅助点的位置作为初始值并固定
}
void FixedUpdate()
{
//运用了倒计时的作用
endTime = Mathf.MoveTowards(endTime, 0, 0.1f);
if (endTime == 0)
{
BallJump();
endTime = 2;
}
}
private void BallJump()
{
UpDown = Random.Range(-1, 1f) * 5;
Ball = new Vector2(transform.position.x, YPell +UpDown );
transform.position = Vector2.Lerp(transform.position, Ball, 0.05f);
}
}
🎶(金)炮管的旋转和发射
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//----------------------
//--作用: 炮管旋转和发射
//----------------------
public class ShootRotation : MonoBehaviour
{
private float Angle;
private bool swtich; //step1:设置使用开关(Bool)
void Start()
{
}
void Update()
{
if(swtich == true )
{
Angle = Input.mouseScrollDelta.y*5;
transform.localRotation = new Quaternion(0, 0, Angle, 0);
}
}
//step2:设置触发检测
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.transform.tag =="Player")
{
swtich = true;
}
}
}
🎶(电)人物的移动和法球的生成
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++)
{
//1.法球生成
AllIea[i] = Instantiate<GameObject>(profab, Allpoint[i].position, Quaternion.identity);
if (i == 3) //3.法球渲染层级设置
{
AllIea[i].GetComponent<SpriteRenderer>().sortingOrder = 3;
}
//2.实现法球移动效果
IeaMove mation = AllIea[i].GetComponent<IeaMove>();//给实例化的物体添加上了脚本IeaMove
mation.Pball = Allpoint[i + 4] ;
}
}
}
🎶(水)运动状态的跟随移动
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.Rendering;
//-----------------------
//--作用:运动状态法球的跟随移动(以及Crush情况)
//-----------------------
public class BallMove : MonoBehaviour
{
// Start is called before the first frame update
private GameObject fab1,fab2; //首先声明两个不同层级的法球预制体
public Transform [] emptyP = new Transform[5]; //为储存四个辅助点的位置
private GameObject [] Fball = new GameObject[5]; //为生成四个实例化法球做载体
private IeaMove[] BALL = new IeaMove[5] ;
public Transform stopBall; //声明恶魔效果散落位置的母体
public bool switchCrush = false;
void Start()
{
//使用资源加载的API
fab1 = Resources.Load<GameObject>(@"prefab1/iea1");
fab2 = Resources.Load<GameObject>(@"prefab1/iea2");
for (int i = 1; i < emptyP.Length; i++)
{
emptyP[i] = transform.GetChild(i);
}
creatMove();
}
private void Update()
{
if (switchCrush == true )
{
print("碰到恶魔果实!");
CrushBall();
}
}
private void creatMove()
{
for (int i = 1; i < emptyP.Length; i++)
{
//使用判断语句的目的就是分两部分实例化法球,高层及和低层级
if (i < 3)
{
Fball[i] = Instantiate<GameObject>(fab1, emptyP[i].position, Quaternion.identity);
}
else if(i>=3)
{
Fball[i] = Instantiate<GameObject>(fab2, emptyP[i].position, Quaternion.identity);
}
BALL[i] = Fball[i].AddComponent<IeaMove>(); //给物体添加脚本
BALL[i].Pball = emptyP[i];
}
}
private void CrushBall() // 更新法球停留的辅助点位置
{
for(int i = 1; i<BALL.Length; i++ )
{
//四散的效果
Fball[i]. transform.Translate(Fball[i].transform .position .x , -2, 0);
BALL[i].Pball = stopBall.GetChild(i-1);
BALL[i].speed = 0.1f;
print("法球失散!");
BALL[i].swtich = true;
BALL[i].M_sprite = Resources.Load<Sprite>(@"prefab1/wow"+(i+1));
}
switchCrush = false;
}
}
🎶(霾)人物的移动
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.Rendering;
using UnityEngine;
//-----------------------
//--作用:mango的移动
//-----------------------
public class mangoMove : MonoBehaviour
{
// Start is called before the first frame update
private float x;
private float y;
private Rigidbody2D Rmango;
private Vector2 mangoMovex,mangoMovey;
private float SpeedVauel = 5;
public float JumpSpeed = 5000;
private Animator MGanimator;
private bool isGrounp,Switch1;
private GameObject bow;
void Start()
{
Rmango = GetComponent<Rigidbody2D>();
MGanimator = GetComponent<Animator>();
}
private void Update()
{
x = Input.GetAxis("Horizontal"); // 按AD键获取类型为Float的数值作为系数范围为【-1,1】
AmationJump();
}
void FixedUpdate()
{
Move();
closeShoot();
}
private void Move() //Mango的移动
{
//实现转向
if (x > 0) //当按下A的时候 x是负数 ,按下D的时候x是正数
{
transform.localScale = new Vector3(0.75f, 0.75f, 1);
}
else if (x < 0)
{
transform.localScale = new Vector3(-0.75f, 0.75f, 1);
}
//通过刚体组件实现物体的移动,我们只需要将刚体速度的大小和方向进行赋值即可
//mangoMovex 和 mangoMoveY 都是vector2 类型的变量
mangoMovex = Vector2.right * x * SpeedVauel; //x轴的速度
mangoMovey = new Vector2(0, Rmango.velocity.y); //y轴的速度
//速度向量的合成,有大小并且有方向的
Rmango.velocity = mangoMovex + mangoMovey;
MGanimator.SetFloat("Run", Mathf.Abs(Rmango .velocity .x));
}
private void AmationJump() //跳跃动画切换功能
{
//当按下空格键和 符合 在地面的条件时
if (Input.GetKeyDown(KeyCode.Space) && isGrounp == true )
{
Rmango.AddForce (Vector2 .up *JumpSpeed);
}
}
private void closeShoot() //法炮发射功能
{
if (Input.GetKeyDown(KeyCode.K))
{
MGanimator.CrossFade("Attack", 0.2f);
bow = Instantiate(Resources.Load<GameObject>(@"prefab1/bow"), transform.GetChild(6).position, Quaternion.identity);
Rigidbody2D dd = new Rigidbody2D();
dd = bow.AddComponent<Rigidbody2D>();
dd.AddForce(new Vector2(transform.localScale.x * 5000, transform.position.y));
MGanimator.SetBool("Attack", false);
}
}
private void damage(bool swtich) //Mango受伤动画的切换
{
print("来切换动画了");
if(swtich == true)
{
MGanimator.SetBool("hurt", true);
}
else
{
MGanimator.SetBool("hurt", false);
}
}
//碰撞器方法
private void OnCollisionStay2D(Collision2D collision)
{
//接触的物体标签为Grounp的时候
if (collision.contactCount == 1&& collision.transform.tag == "grounp" )
{
isGrounp = collision.gameObject.CompareTag("grounp");
MGanimator.SetFloat("Jump", 0);
}
if (collision.transform.tag == "damage")
{
damage(true);
print("注意,正在接触陷阱");
}
}
private void OnCollisionExit2D(Collision2D collision) //也可以说是跳跃状态的时候
{
if (collision.transform.tag == "grounp" && collision.contactCount == 0)
{
MGanimator.SetFloat("Jump", 3);
}
isGrounp = false;
if (collision.transform.tag == "damage")
{
damage(false);
print("退出了");
}
}
}
⭐相关文章⭐
⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作
⭐【2023unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!