以下是一个简单的玩家控制代码示例,包含跳跃、奔跑、行走和攻击功能:

public class PlayerController : MonoBehaviour {

    // 移动速度
    public float moveSpeed = 5f;
    // 跳跃力量
    public float jumpForce = 5f;
    // 攻击力量
    public float attackPower = 10f;
    // 攻击范围
    public float attackRange = 0.5f;

    // 是否在地面上
    private bool isGrounded = true;
    // 是否正在奔跑
    private bool isRunning = false;
    // 是否正在攻击
    private bool isAttacking = false;

    // 动画控制器组件
    private Animator animator;
    // 刚体组件
    private Rigidbody2D rigidBody;
    // 碰撞体组件
    private CapsuleCollider2D capsuleCollider;

    void Start () {
        animator = GetComponent<Animator>();
        rigidBody = GetComponent<Rigidbody2D>();
        capsuleCollider = GetComponent<CapsuleCollider2D>();
    }
    
    void Update () {
        // 获取水平移动方向
        float moveDirection = Input.GetAxisRaw("Horizontal");
        // 如果移动方向不为0,则移动玩家
        if (moveDirection != 0) {
            Move(moveDirection);
        }
        // 如果按下跳跃键并且在地面上,则跳跃
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded) {
            Jump();
        }
        // 如果按下攻击键并且没有正在攻击,则攻击
        if (Input.GetKeyDown(KeyCode.Z) && !isAttacking) {
            Attack();
        }
        // 更新动画状态
        UpdateAnimation();
    }

    // 移动玩家
    void Move(float direction) {
        // 设置水平移动速度
        float moveSpeedX = isRunning ? moveSpeed * 2 : moveSpeed;
        rigidBody.velocity = new Vector2(direction * moveSpeedX, rigidBody.velocity.y);
        // 翻转玩家的朝向
        transform.localScale = new Vector3(direction > 0 ? 1 : -1, 1, 1);
    }

    // 跳跃
    void Jump() {
        isGrounded = false;
        rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpForce);
    }

    // 攻击
    void Attack() {
        isAttacking = true;
        // 检测攻击范围内是否有敌人
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, attackRange);
        foreach (Collider2D collider in colliders) {
            if (collider.CompareTag("Enemy")) {
                // 对敌人造成伤害
                collider.GetComponent<EnemyController>().TakeDamage(attackPower);
            }
        }
        // 攻击动画结束后,重置攻击状态
        Invoke("ResetAttack", 0.5f);
    }

    // 重置攻击状态
    void ResetAttack() {
        isAttacking = false;
    }

    // 更新动画状态
    void UpdateAnimation() {
        animator.SetBool("isGrounded", isGrounded);
        animator.SetBool("isRunning", isRunning);
        animator.SetBool("isAttacking", isAttacking);
    }

    // 碰撞检测
    void OnCollisionEnter2D(Collision2D collision) {
        if (collision.gameObject.CompareTag("Ground")) {
            isGrounded = true;
        }
    }

    // 触发检测
    void OnTriggerEnter2D(Collider2D collider) {
        if (collider.gameObject.CompareTag("RunTrigger")) {
            isRunning = true;
        }
    }

    void OnTriggerExit2D(Collider2D collider) {
        if (collider.gameObject.CompareTag("RunTrigger")) {
            isRunning = false;
        }
    }

}

请注意,这只是一个简单的示例代码,您需要根据您的游戏需求进行修改和优化。同时,该代码只包含基本的玩家控制功能,您需要自行添加更多的功能和细节来丰富游戏体验。

Unity 黑魂风格游戏玩家控制代码示例 - 跳跃、奔跑、行走和攻击

原文地址: https://www.cveoy.top/t/topic/mBVc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录