0
点赞
收藏
分享

微信扫一扫

Unity项目-黑魂复刻(三)玩家控制器(跳跃)

迎月兮 2022-02-21 阅读 29

新增跳落动画

在这里插入图片描述

新增落地侦查器

在这里插入图片描述

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

public class OnGroundSensor : MonoBehaviour
{
    //获得人物胶囊体
    public CapsuleCollider capcol;

    //上半球的球心
    private Vector3 point1;
    //下半球的球心
    private Vector3 point2;
    //胶囊体的半径
    private float radius;
    void Awake()
    {
        radius = capcol.radius;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        //transform.position在底部
        point1 = transform.position + transform.up * radius;
        point1 = transform.position + transform.up * capcol.height - transform.up * radius;

        //获得layer为Ground的所有碰撞体
        Collider[] outputCols = Physics.OverlapCapsule(point1, point2, radius, LayerMask.GetMask("Ground"));
        if (outputCols.Length != 0) {
            foreach (var col in outputCols)
            {
                print("collision:" + col.name);

            }  
        }
    }
}

使用落地侦查器

在这里插入图片描述

OnGroundSensor脚本中

//获得layer为Ground的所有碰撞体
        Collider[] outputCols = Physics.OverlapCapsule(point1, point2, radius, LayerMask.GetMask("Ground"));
        if (outputCols.Length != 0)
        {
            //foreach (var col in outputCols)
            //{
            //    print("collision:" + col.name);

            //}  
            SendMessageUpwards("IsGround");
        }
        else {
            SendMessageUpwards("IsNotGround");


        }

ActorController脚本中

    void IsGround() {
        print("is on ground");
        anim.SetBool("isGround", true);
    }
    void IsNotGround() {

        print("is not on ground!!!!");
        anim.SetBool("isGround", false);
    }

修改人物在执行下落动画时出现垂直降落问题

 void OnGroundEnter() {
        pi.inputEnabled = true;
        lockPlanar = false;
    
    }

在这里插入图片描述
小bug:

public class OnGroundSensor : MonoBehaviour
{
 //获得人物胶囊体
    public CapsuleCollider capcol;
    //让自定义的胶囊体向下偏移
    public float offset = 0.1f;
    //上半球的球心
    private Vector3 point1;
    //下半球的球心
    private Vector3 point2;
    //胶囊体的半径
    private float radius;
	//让自定义的胶囊体向下偏移
    public float offset = 0.1f;
 	void Awake()
    {
        //使胶囊体变窄
        radius = capcol.radius-0.1f;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        //transform.position在底部
        point1 = transform.position + transform.up * (radius-offset);
        point2 = transform.position + transform.up * (capcol.height- offset) - transform.up * radius
        ...
    }
}

在这里插入图片描述

举报

相关推荐

0 条评论