角色移动优化【Unity2D自学之路】

自学unity2D独立游戏开发,第一篇自学笔记。在场景中添加角色,并给角色添加Rigidbody2D刚体组件、collection2D碰撞体组件,c#脚本组件控制人物移动和跳跃。c#脚本组件内容如下,我进行了详细的注释,以便后用,想学习的朋友可以参考一下。

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

public class PlayerControl : MonoBehaviour
{       
    private Rigidbody2D m_PlayerRb;// 人物刚体
    private float m_FacingRight = 1;//角色是否面向右方
    private float m_MoveInputDirection;//方向输入
    public float m_MoveSpeed;//移动速度
    private Vector3 m_Velocity = Vector3.zero;
    private float m_MovementSmoothing = .05f;//移动平滑速度

    public Transform m_GroundCheck;//地面检查
    public bool m_Grounded=true;//是否站在地面上主要用于跳跃是判断
    public float m_JumpForce = 15f;//跳跃力量
    public float groundCheckRadius = 0.2f;//地面检测半径
    public LayerMask m_WhatisGround;//检测碰撞的层

    [SerializeField]private float jumpFactor;
    private void Awake()
    {
        m_PlayerRb = GetComponent<Rigidbody2D>();//获取角色刚体
    }
    private void Update()
    {
        //获取角色图片大小sp.bounds.extents.y
        m_Grounded = Physics2D.OverlapCircle(m_GroundCheck.position, groundCheckRadius, m_WhatisGround);
        Vector3 m_mousePostion = Input.mousePosition;//获取鼠标位置
        m_mousePostion = Camera.main.ScreenToWorldPoint(m_mousePostion);//转换成世界坐标
        // 因为是2D,用不到z轴。使将z轴的值为0,这样鼠标的坐标就在(x,y)平面上了
        m_mousePostion.z = 0;
        //根据鼠标位置翻转角色
        if (m_mousePostion.x-transform.position.x < 0 && m_FacingRight == 1)
        {
            Flip();
        }
        else if (m_mousePostion.x - transform.position.x > 0 && m_FacingRight == -1)
        {
            Flip();
        }

        #region 左右移动相关
        //获取键盘输入,表示左右输入 按键A或D或向左箭头向右箭头
        m_MoveInputDirection = Input.GetAxisRaw("Horizontal");       
        #endregion
        #region 跳跃相关
        //判断是否按下跳跃键,并角色在地面上
        if (Input.GetButtonDown("Jump")&&m_Grounded)
        {
            m_Grounded = false;//跳跃后,是否在地面为否
            m_PlayerRb.velocity = Vector2.up * m_JumpForce;//角色向上跳跃
        }
        BetterJump();
        #endregion
    }
    private void FixedUpdate()
    {
        if (m_MoveInputDirection!=0)//表示玩家按下了左右方向键
        {
            //移动角色
            Vector3 targetVelocity = new Vector2(m_MoveSpeed * m_MoveInputDirection, m_PlayerRb.velocity.y);
            m_PlayerRb.velocity = Vector3.SmoothDamp(m_PlayerRb.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
        }
    }
    /// <summary>
    /// 角色翻转
    /// </summary>
    void Flip()
    {
        m_FacingRight *= -1;
        transform.Rotate(0.0f, 180.0f, 0.0f);
    }

    /// <summary>
    /// 角色跳跃,长按高跳,短按低跳
    /// </summary>
    private void BetterJump()
    {
        //角色跳起后下落阶段
        if (m_PlayerRb.velocity.y<0)
        {
            m_PlayerRb.velocity += Vector2.up * Physics2D.gravity.y * jumpFactor*Time.deltaTime;//不停的增加重力,手感更强使游戏更有可玩性
        }
        else if (m_PlayerRb.velocity.y>0&&!Input.GetButton("Jump"))//角色跳起阶段,并放弃按跳跃键
        {
            m_PlayerRb.velocity += Vector2.up * Physics2D.gravity.y * jumpFactor * Time.deltaTime;
        }
    }

    private void OnDrawGizmos()
    {
        //地面检测方法划线显示,便于观察
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(m_GroundCheck.position, groundCheckRadius);
        
    }

}
posted on 2020-04-04 21:21  一切从头再来  阅读(707)  评论(0编辑  收藏  举报