Unity 代码无法缩进,有时会出现故障

     2023-04-12     79

关键词:

【中文标题】Unity 代码无法缩进,有时会出现故障【英文标题】:Unity Code not working as indented, Sometimes a Glitch occurs 【发布时间】:2021-09-27 18:29:49 【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JamesController : MonoBehaviour 

public static JamesController instance;

public GameObject particle;

[SerializeField]
public float speed;
bool started;
bool gameOver;
public Rigidbody rb;
Animator anim;

void Awake()

    if (instance == null)
    
        instance = this;
    


void Start()

    rb = this.GetComponent<Rigidbody>();
    anim = this.GetComponent<Animator>();
    started = false;
    speed = 7;


void Update()


    // if (!started)
    // 
    //     if (Input.GetMouseButtonDown(0))
    //     
    //         rb.velocity = new Vector3(speed, 0, 0);
    //         started = true;

    //         GameManager.instance.StartGame();
    //         anim.SetTrigger("Start");
    //     
    // 

    if (rb.position.y < 0.45 && !gameOver)
    
        gameOver = true;
        rb.velocity = new Vector3(0, -25f, 0);

        Camera.main.GetComponent<CameraFollowJames>().gameOver = true;
        Camera.main.GetComponent<CameraFollowAJ>().gameOver = true;
        Camera.main.GetComponent<CameraFollowRemy>().gameOver = true;

        GameManager.instance.GameOver();
        // CancelInvoke("IncreaseSpeed");
    

    if (started)
    
        if (Input.GetMouseButtonDown(0) && !gameOver)
        
            SwitchDirection();
        

        // InvokeRepeating("IncreaseSpeed", 0.1f, 1f);
    


public void Started()

    if (!started)
    
        started = true;
        rb.velocity = new Vector3(speed, 0, 0);

        GameManager.instance.StartGame();
        anim.SetTrigger("Start");
    


void SwitchDirection()

    if (rb.velocity.z != 0)
    
        rb.velocity = new Vector3(speed, 0, 0);
        transform.Rotate(0, 90, 0);
    
    else if (rb.velocity.x != 0)
    
        rb.velocity = new Vector3(0, 0, speed);
        transform.Rotate(0, 270, 0);
    
    else if (rb.velocity == Vector3.zero)
    
        rb.velocity = new Vector3(speed, 0, 0);
    


void OnTriggerEnter(Collider col)

    if (col.gameObject.tag == "Diamond")
    
        GameObject.Find("ScoreManager").GetComponent<ScoreManager>().score += 3;
        GameObject.Find("ScoreManager").GetComponent<ScoreManager>().diamondCount += 1;
        GameObject part = Instantiate(particle, col.gameObject.transform.position, particle.transform.rotation) as GameObject;
        Destroy(col.gameObject);
        Destroy(part, 1f);
    


// void IncreaseSpeed()
// 
//     speed = speed + 0.00005f;
// 

根据我的理论,开关方向应该可以正常工作,但有时会出现故障。 请看下面的视频: https://drive.google.com/file/d/1eYY2GYYZASXukg9RARzNQYlk1q2zwcBV/view?usp=sharing

【问题讨论】:

你所说的故障是指当你期望他转动时,角色不会转动,对吗? 其实方向错了,就是故障 【参考方案1】:

为此使用预定义的旋转和速度可能比使用附加的 transform.Rotate 更容易。

public class JamesController : MonoBehavior

    public Direction[] directions;
    public int directionIndex = 0;
    
    void Start()
        SetDirection(directionIndex);
    

    void FixedUpdate()
        rb.velocity = directions[directionIndex].velocity;
    

    void SetDirection(int index)
        directionIndex = index;
        rb.velocity = directions[directionIndex].velocity;
        transform.rotation = Quaternion.Euler(directions[directionIndex].eulerDirection);
    

    void SwitchDirection()
    
        directionIndex += 1;
        if(directionIndex >= directions.Length)
            directionIndex = 0;
        
        if(directionIndex < 0)
            directionIndex = directions.Length - 1;
            
        SetDirection(directionIndex);
    


[System.Serializable]
public class Direction 

    public Vector3 velocity;
    public Vector3 eulerDirection;

【讨论】:

好吧,我解决了这个问题,只是使用 bool 而不是确定方向的 If else 语句。 这也可以,但是我建议在这种情况下设置旋转而不是使用旋转。如果不是为了其他任何事情,那么要避免由于浮点错误和诸如此类的任何漂移,因为 transform.rotate 是加法的。 嗯,这个主意不错,我一定会试试的。

无法处理故障异常

...出现故障。我已经说过我的服务合同可能有问题。下面是代码;publicvoidFoo()tryDoSomething();//throwsFaultException<FooFault>catch(FaultException)thro 查看详情

unity里面导入模型或素材电脑就蓝屏是怎么情况呀

...硬件兼容列表检查自己的硬件。还有就是,如果主板BIOS无法支持大容量硬盘也会 查看详情

ChannelFactory 会出现故障吗?

...)处置。我是否需要担心缓存的ChannelFactory出现故障从而无法用于创建新频道的情况?如果是这样, 查看详情

电脑无法启动是啥原因

电脑无法启动原因有以下几种原因:1、电源开关故障:当电源开关按键因为老化而导致电源开关按钮按下后不能及时复位,使开关始终处于接通状态,这时就会出现按下电源开关后,主机加电开始启动,但过4秒后自动关机的现... 查看详情

故障公告10:30-10:45左右dockerswarm集群节点问题引发故障

...问出现502或500,当时我们就判断是某个节点出现问题,但无法直接定位出哪个节点,只能依次将一个个节点下线-上线。但是,不走运的是出现问题的节点恰恰是我们最后下线的一个节点。所以,在下线正常节点的过程中,更多... 查看详情

倍加福编码器绿灯亮红灯闪烁故障

1.旋转编码器无法产生正确波形:这个问题是旋转编码器自身出现故障,比如内部组件损坏,致使无法产生正确的波形,最终导致不能正常工作.解决方案:如果旋转编码器出现这种情况,解决的办法是更换编码器。2.旋转编码器端子... 查看详情

python:期望缩进块(代码片段)

...序,每当我运行它们时都会出现错误。下面是2个文件的代码。它一直告诉我,在两个程序的main(sys.argv)命令中都会出现缩进块错误。任何指导都将得到推广。mapper.朋友#!/usr/bin/envpython#Besuretheindentationisidenticalandalsobesurethelineabov... 查看详情

win10蓝屏提示yourpc/deviceneedstoberepaired

...ows下经常出现蓝屏故障 出现此类故障的表现方式多样,有时在Windows启动时出现,有时在Windows下运行一些软件时出现,出现此类故障一般是由于用户操作不当促使Windows系统损坏造成,此类现象具体表现在以安全模式引导时不能... 查看详情

SSL 证书有时会停止工作

...服务器凭据私钥时发生致命错误。从加密模块返回的错误代码是0x8009030d。内部错误状态为10001。目前为了解决这个问题,我们只需将故障网站的SSL绑定更改为不同的网站,保存并切换 查看详情

unity中协程函数没有错误为啥会报错

...,如UI组件的重叠等问题,需要排除这些问题。3可能存在代码逻辑错误,需要仔细检查代码。延伸:在使用协程函数时, 查看详情

为什么这个c++多线程互斥代码偶尔会出现故障?(代码片段)

我在linuxDebian系统上使用下面的foo.cpp代码:#include<iostream>#include<mutex>#include<condition_variable>#include<chrono>#include<thread>std::mutexmtx;std::condition_variablecvar;long 查看详情

无法及时发现设备故障还是你的痛处吗?

...C部分ECS服务器等实例出现IOHANG,这导致了很多网站和APP无法正常使用,阿里云的工作人员经紧急排查处理后逐步恢复,此外将根据协议尽快赔偿。可见,设备故障对任何公司都会造成不良影响,它会中断公司的相关业务,造成... 查看详情

ejabberd 集群:多主或主从

...题,即使在修复主服务器后(如果它出现故障),集群也无法再次运行.此外,有时,必须再次访问每个从站的ejabberd实例以使其正 查看详情

使用 ietd 从 iSCSI 目标引导的 Windows 故障转移

...立即重新启动。到那时,Windows似乎被“冻结”了。蓝屏代码有时是0x00000f4,有时是0x000007a。现在,在故障转移时,辅助节点 查看详情

为啥有时pycharm会无法完成代码补全或代码提示

参考技术A很多输入法,和pycharm快捷键设置有冲突,你切换下输入法试试,我把搜狗输入法切换了下就可以了 查看详情

第 5 行出现意外缩进?

...非常简单的程序。好像当我在if语句之后放了两行以上的代码,就会出现问题。你能帮我解决这个问题吗?非常感谢你x=raw_input(\'Givemeanumbe 查看详情

硬盘有问题一般会造成哪些计算机故障

一.鼠标等待转圈圈二.复制文件掉速度有时为0卡住三.系统运行卡住之后自动关机四.系统盘有问题出现蓝屏五.开机找不到硬盘 查看详情

局域网无法访问网站的原因

...答,我感觉问题是在于DNS服务器的问题网络设置不当引起无法打开网页HOSTS文件的问题等我是河南网通的客户麻烦大家再认真帮我分析下我等下去机房调试下...河南网通DNS202.102.224.68我的配置正常当IE浏览器本身出现故障时,自然... 查看详情