Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
PlayerAimSwordState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//从地面状态进入
public class PlayerAimSwordState : PlayerState
{public PlayerAimSwordState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}public override void Enter(){base.Enter();player.skill.sword.DotsActive(true);}public override void Exit(){base.Exit();}public override void Update(){base.Update();if(Input.GetKeyUp(KeyCode.Mouse1))//放开右键,投出{stateMachine.ChangeState(player.idleState);}}
}
Sword_Skill.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Sword_Skill : Skill
{[Header("Skill Info")][SerializeField] private GameObject swordPrefab;//sword预制体[SerializeField] private Vector2 launchForce;//发射力度[SerializeField] private float swordGravity;//发射体的重力private Vector2 finalDir;//发射方向[Header("Aim dots")][SerializeField] private int numberOfDots;//需要的点的数量[SerializeField] private float spaceBetweenDots;//相隔的距离[SerializeField] private GameObject dotPrefab;//dot预制体[SerializeField] private Transform dotsParent;//不是很懂private GameObject[] dots;//dot组protected override void Start(){base.Start();GenerateDots();//生成点函数}protected virtual void Update(){if (Input.GetKeyUp(KeyCode.Mouse1)){finalDir = new Vector2(AimDirection().normalized.x * launchForce.x, AimDirection().normalized.y * launchForce.y);//将位移量改为单位向量分别与力度的x,y相乘作为finalDir}if(Input.GetKey(KeyCode.Mouse1)){for (int i = 0; i < dots.Length;i++){dots[i].transform.position = DotsPosition(i * spaceBetweenDots);//用循环为每个点以返回值赋值(传入值为每个点的顺序i*点间距}}}public void CreateSword(){GameObject newSword = Instantiate(swordPrefab, player.transform.position, transform.rotation);//创造实例,初始位置为此时player的位置Sword_Skill_Controller newSwordScript = newSword.GetComponent<Sword_Skill_Controller>();//获得ControllernewSwordScript.SetupSword(finalDir, swordGravity);//调用Controller里的SetupSword函数,给予其速度和重力DotsActive(false);//关闭点的显示}public Vector2 AimDirection(){Vector2 playerPosition = player.transform.position;//拿到玩家此时的位置Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);//https://docs.unity3d.com/cn/current/ScriptReference/Camera.ScreenToWorldPoint.html//大概就是返回屏幕上括号里的参数的位置,这里返回了鼠标的位置//拿到此时鼠标的位置Vector2 direction = mousePosition - playerPosition;//获得距离的绝对向量return direction;//返回距离向量}public void DotsActive(bool _isActive){for(int i = 0;i<dots.Length;i++){dots[i].SetActive(_isActive);//设置每个点是否显示函数}}private void GenerateDots()//生成点函数{dots = new GameObject[numberOfDots];//为dot赋予实例数量for(int i = 0;i < numberOfDots;i++){dots[i] = Instantiate(dotPrefab, player.transform.position, Quaternion.identity,dotsParent);//对象与世界轴或父轴完全对齐//https://docs.unity3d.com/cn/current/ScriptReference/Quaternion-identity.htmldots[i].SetActive(false);//关闭dot}}private Vector2 DotsPosition(float t)//传入顺序相关的点间距{Vector2 position = (Vector2)player.transform.position + new Vector2(AimDirection().normalized.x * launchForce.x,AimDirection().normalized.y * launchForce.y) * t + .5f * (Physics2D.gravity * swordGravity) * (t * t);//t是控制之间点间距的return position;//返回位置}//设置点间距函数
}