Unity类银河恶魔城学习记录7-3 P69 Setting up sword‘s aim源代码

 Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码

Unity类银河恶魔城学习记录7-3 P69 Setting up sword‘s aim源代码_第1张图片

【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();//获得Controller
        newSwordScript.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

你可能感兴趣的:(学习,游戏引擎,C#,Unity,unity)