You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity无尽跑酷项目异常:触碰金币时玩家被销毁且分数不统计

问题修复方案

核心问题定位

  • 玩家触碰金币时自身被销毁:OnTriggerEnter 中 Destroy(gameObject) 未被包裹在敌人碰撞的代码块内,导致所有碰撞触发都会执行玩家销毁逻辑。
  • 分数统计异常:更新分数文本的代码写在分数递增之前,显示的分数始终落后实际值1分。
  • 金币未销毁:因玩家被提前销毁,金币销毁逻辑可能未执行(核心还是前两个问题导致连锁反应)。

修复后的完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
    Animator myani;
    Rigidbody myrb;
    public Text scoretext;
    int score = 0;

    void Start()
    {
        myani = GetComponent<Animator>();
        myrb = GetComponent<Rigidbody>();
        UpdateScoreText();
    }

    void Update()
    {
        float currentX = transform.position.x;
        // 左右移动逻辑优化:用Mathf.Approximates规避浮点数精度问题
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (Mathf.Approximately(currentX, 0))
            {
                transform.position = new Vector3(2, 0.5f, 0);
            }
            else if (Mathf.Approximately(currentX, -2))
            {
                transform.position = new Vector3(0, 0.5f, 0);
            }
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (Mathf.Approximately(currentX, 0))
            {
                transform.position = new Vector3(-2, 0.5f, 0);
            }
            else if (Mathf.Approximately(currentX, 2))
            {
                transform.position = new Vector3(0, 0.5f, 0);
            }
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            myani.SetTrigger("Roll");
        }
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            myani.SetTrigger("Jump");
            // 合并跳跃物理逻辑,减少重复输入检测
            if (Mathf.Approximately(myrb.velocity.y, 0))
            {
                myrb.velocity = new Vector3(0, 5f, 0);
            }
        }
        if (Input.GetKeyDown(KeyCode.End))
        {
            myani.SetTrigger("Run 1");
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("coin"))
        {
            score += 1;
            UpdateScoreText();
            Destroy(other.gameObject);
        }

        if (other.gameObject.CompareTag("enemy"))
        {
            SceneManager.LoadScene("Game over");
            Destroy(gameObject);
        }
    }

    // 封装分数更新方法,避免重复代码
    private void UpdateScoreText()
    {
        scoretext.text = $"Score: {score}";
    }
}

关键修复说明

  1. 玩家误销毁修复:将 Destroy(gameObject) 移入敌人碰撞的 if 代码块内,确保仅触碰敌人时销毁玩家。
  2. 分数统计修正:先执行分数递增再更新文本,保证显示最新分数;封装 UpdateScoreText 方法简化代码维护。
  3. 移动逻辑优化:用 Mathf.Approximately 替代直接浮点数比较,避免精度误差导致移动失效。
  4. 代码规范调整:类名改为 Player(符合C#命名规范),使用字符串插值让文本拼接更清晰。

内容的提问来源于stack exchange,提问作者chakri 108

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 15:17:27