Unity 3D RPG 个人学习笔记

在Asset文件夹下创建Script文件夹,用来存放所创建的脚本,打开文件夹,右键create-c#script,创建代码,重命名为PlayerInput。双击打开脚本,代码如下:

检测键盘输入

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerInput : MonoBehaviour

{

    public float horizontalInput;

    public float verticalInput;

    // Start is called before the first frame update

   

    // Update is called once per frame

    void Update()

    {

        horizontalInput=Input.GetAxis("Horizontal");

        verticalInput=Input.GetAxis("Vertical");

        //检测水平输入和垂直输入

    }

    /// <summary>

    /// 当角色死亡(禁用)是调用OnDisable方法,关闭输入

    /// </summary>

   private void OnDisable() {

    horizontalInput=0;

    verticalInput=0;

   }

}

补充:Horizontal与unity中edit-project settings-Input Manager图示名称相同

实现角色移动(水平方向和垂直方向)

using System.Collections;

using System.Collections.Generic;

using Unity.VisualScripting;

using UnityEngine;

public class Character : MonoBehaviour

{

    private CharacterController cc;//角色进行移动,通常使用character controller组件,rigidbody通常用来实现物理交互

    public float speed = 10f;

    private Vector3 moveVelocity;

    private PlayerInput playerInput;

    private float verticalVelocity;

    private float Gravity=-9.8f;

    private Animator animator;

   

   private void Awake() {

    cc=GetComponent<CharacterController>();

    playerInput=GetComponent<PlayerInput>();

    animator=GetComponent<Animator>(); //组件获取

   }

       private void CalculateMove(){

        moveVelocity.Set(playerInput.horizontalInput,0,playerInput.verticalInput);//获取输入的值并将其转化为三维向量

        moveVelocity.Normalize();//向量的归一化

        moveVelocity=Quaternion.Euler(0,-45,0)*moveVelocity;//实现对向量的旋转(与相机旋转角度相同)

        moveVelocity*=speed*Time.deltaTime;

        animator.SetFloat("Speed",moveVelocity.magnitude);//将物体当前的速度大小(即moveVelocity的模长)赋值给Animator控制器中的名为"Speed"的动画参数,moveVelocity.magnitue获取moveVelocity的模长

        //为了防止player持续转向,判断moveVelocity是否为零,若为零则不进行旋转

        if(moveVelocity!=Vector3.zero){

            transform.rotation=Quaternion.LookRotation(moveVelocity);

        }

       

       }

       

   

   

       

       

   

   

    //这段代码的主要目的是在每一帧的固定更新阶段,先计算出对象应有的速度,然后基于此速度信息来实际移动游戏对象。

    void FixedUpdate(){

        CalculateMove();

        //如果对象没有与地面接触,那么它的速度为重力加速度,否则速度为重力加速度的0.3倍

        if(cc.isGrounded==false){

            verticalVelocity=Gravity;

        }

        else{

            verticalVelocity=Gravity*0.3f;//判断当角色在地面时,依旧施加力的原因是角色控制器的设计中,在角色仍在地面上时,下一帧角色仍会浮在地面上,isGrounded=false,所以需要在重力加速度上施加一个小的力

        }

           

            moveVelocity+=verticalVelocity*Vector3.up*Time.deltaTime;//将垂直方向的力与水平方向的力矢量相加,将verticalVelocity转化成三维向量

        cc.Move(moveVelocity);   //移动游戏对象


 

    }

   

}

设置动画控制器

Idle动画设置

Game文件夹下创建一个Animator文件夹,用来存放动画控制器,右键-create-Animator comtroller,双击打开。

点击idle,motion选项为idle的动画,点击红圈所在位置,跳转到project对应位置,

点击对应的动画,点击edit,

选中loop time,保证动画可以循环播放,

给player添加一个动画控制器,如图设置

run状态设置以及状态转换

在Base Layer界面右键Create State-Empty,单击创建出来的状态,重命名为run,motion选择为run的动画,点击LittleAdventureAndie,Project会跳转到所在位置,

点击LittleAdventureAndie,Project会跳转到所在位置,点击对应动画,点击edit,选中loop time

状态转换

右键Idle,make transition,与run状态连接,点击连接线,如图

取消has exit time,transition duration设置为0.1,parameter增加一个float属性的speed,

condition如上图设置

run->walk同理可实现,conditon设置为less,代码实现

    private Animator animator;

        animator.SetFloat("Speed",moveVelocity.magnitude);//将物体当前的速度大小(即moveVelocity的模长)赋值给Animator控制器中的名为"Speed"的动画参数,moveVelocity.magnitue获取moveVelocity的模长

Fall状态设置

设置如图

代码:        animator.SetBool("AirBorne",!cc.isGrounded);//调用animator的bool参数"AirBorne",若cc.isGrounded为false,即角色没有与地面接触,则将bool参数设置为true,否则为false

装备武器

参数设置如图所示

左手:(-0.0004,0.002,-0.003)

右手:(-0.0006,0.002,-0.003)

脚步特效设置

在SCripts文件夹下创建AnimatorBehavior文件夹,创建RunBehavior脚本

在scripts文件夹下创建playerVFXManager脚本,管理VFX

创建VFX游戏对象

vfx foot step 如下图设置

ps:删除initial event name输入框中的内容,避免初始状态就播放脚步特效

代码如下

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.VFX;//引入VFX

public class PlayerVFXManager : MonoBehaviour

   

{

    public VisualEffect visualEffect;//定义变量visualEffect

   

    public void UpdateStep(bool state){

        if(state){

            visualEffect.Play();//播放VFX动画

        }

        else{

            visualEffect.Stop();

        }

    }

       

   

}

state是跑步状态,如图设置

add behavior添加一个run behavior的脚本


代码如下

using System.Collections;

using System.Collections.Generic;

using Unity.VisualScripting;

using UnityEngine;

public class RunBehavior : StateMachineBehaviour

{

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)

    {

        if(animator.GetComponent<PlayerVFXManager>() != null)//异常处理,判断PlayVFXManager组件是否为空

       animator.GetComponent<PlayerVFXManager>().UpdateStep(true);//animator对象的GetComponent<PlayerVFXManager>()方法获取PlayVFXManager组件,然后调用UpdateStep方法

    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks

    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)

    //{

    //    

    //}

    //OnStateExit is called when a transition ends and the state machine finishes evaluating this state

    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)

    {

       if(animator.GetComponent<PlayerVFXManager>() != null)

       animator.GetComponent<PlayerVFXManager>().UpdateStep(false);

       

    }

    // OnStateMove is called right after Animator.OnAnimatorMove()

    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)

    //{

    //    // Implement code that processes and affects root motion

    //}

    // OnStateIK is called right after Animator.OnAnimatorIK()

    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)

    //{

    //    // Implement code that sets up animation IK (inverse kinematics)

    //}

}

ps:我们只需要判断state的转换,取消掉state enter exit的注释即可。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值