如何解决Unity 代码无法缩进,有时会出现故障
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);
// 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);
GameManager.instance.StartGame();
anim.SetTrigger("Start");
}
}
void SwitchDirection()
{
if (rb.veLocity.z != 0)
{
rb.veLocity = new Vector3(speed,0);
transform.Rotate(0,90,0);
}
else if (rb.veLocity.x != 0)
{
rb.veLocity = new Vector3(0,speed);
transform.Rotate(0,270,0);
}
else if (rb.veLocity == Vector3.zero)
{
rb.veLocity = new Vector3(speed,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
解决方法
为此使用预定义的旋转和速度可能比使用加性 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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。