【问题标题】:Unity Particle Collision 3D GameUnity粒子碰撞3D游戏
【发布时间】:2021-10-11 23:13:43
【问题描述】:

我正在尝试将粒子添加到我的 Collison,但收到错误消息。我不断从我的代码中得到他的错误,我不知道为什么。错误是: OnGUIDepth 已更改:0 为 1。事件类型为 0 (56,60):错误 CS1001:预期标识符。 (56,55): 错误 CS1003: 语法错误, ',' 预期

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

public class PlayerController1 : MonoBehaviour
{
    public float horizontalInput;
    public float speed = 10.0f;
    public float xRange = 100;
    private Animator playerAnim;
    public AudioClip crashSound;
    private AudioSource playerAudio;

    public GameObject projectilePrefab;
    public ParticleSystem explosionParticle;
    public bool isOnGround = true;
    public bool gameOver;
    


    // Start is called before the first frame update
    void Start()
    {
        playerAudio = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (transform.position.x < -xRange)
        {
            transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
        }

        if (transform.position.x > xRange)
        {
            transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
        }
        else
        {
        }


        horizontalInput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            //Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
            Instantiate(projectilePrefab, transform.position, transform.rotation);
        }


    }

    private void OnCollisionEnter(Collision collision other)
    {
        if (other.gameObject.CampareTag("Rock"))
        {
            isOnGround = true;
        } else if (other.gameObject.CompareTag("Star"))
        {
            Debug.Log("Game Over");
            gameOver = true;
            playerAnim.SetBool("Death_b", true);
            playerAnim.SetInterger("DeathType_int", 1);
            explosionParticle.Play();
            playerAudio.PlayOneShot(crashSound, 1.0f);

        }
    }
}

我的代码有什么需要修复的地方吗?

【问题讨论】:

    标签: c# visual-studio unity3d


    【解决方案1】:

    这里的错误已经由编译器说明并且很容易修复,只需将鼠标悬停在上面,看看intellisense 告诉你什么

    让我们回顾一下这个函数

    private void OnCollisionEnter(Collision collision other)
    {
        if (other.gameObject.CampareTag("Rock"))
        {
            isOnGround = true;
        }
        else if (other.gameObject.CompareTag("Star"))
        {
            Debug.Log("Game Over");
            gameOver = true;
            playerAnim.SetBool("Death_b", true);
            playerAnim.SetInterger("DeathType_int", 1);
            explosionParticle.Play();
            playerAudio.PlayOneShot(crashSound, 1.0f);
        }
    }
    

    在第一行,

    private void OnCollisionEnter(Collision collision other) 
    

    'other' 在那里毫无意义。 Collision 是类型,collision 是标识符。人们经常将 other 作为您可能会感到困惑的标识符。看到你使用 other 作为函数中的变量,你可以从括号中删除冲突。

    该函数进一步只包含拼写错误,例如 .CamareTag 需要为 CompareTag

    请严格遵守语法并使用智能感知,这是一个令人难以置信的工具,可以 100% 消除此类错误。

    另一个错误(如果有错误,看起来只是一个日志)

    OnGUIDepth 已更改:0 为 1。事件类型为 0 (56,60):

    不包含在此脚本中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多