Unity-为什么我的gameObject会闪烁

时间:2018-02-24 17:15:25

标签: c# unity3d camera flicker

所以基本上我有一个平台游戏,我遇到了一些问题 -

(有点癫痫警告!)

https://www.youtube.com/watch?v=ypAzn776-d0

正如您所看到的,屏幕闪烁了很多,而且无法播放。相机移动脚本在这里 -

        //camera rotation
    float mouseX = Input.GetAxis("Mouse Y");
    float mouseY = Input.GetAxis ("Mouse X");

    float rotAmountX = -mouseX * Mousesens;
    float rotAmountY = mouseY * Mousesens;

    Vector3 targetRot = transform.rotation.eulerAngles;
    Vector3 targetRotI = CameraF.transform.rotation.eulerAngles;

    targetRotI.x += rotAmountX;
    targetRot.y += rotAmountY;

    transform.rotation = Quaternion.Euler (targetRotI);
    CameraF.transform.rotation = Quaternion.Euler (targetRot);

CameraF只是相机。如果需要,整个脚本都在这里 -

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

public class PlayerMove : MonoBehaviour {

public float speed=0;
float BackSpeed = 0;

float leftSpeed;
float rightSpeed;

bool pause = false;

public float Mousesens;
public GameObject CameraF;

public Rigidbody rb;
public float jump;
bool jumpwt = false;

bool trickwt = false; 

public GameObject StartPos;
public AudioSource Loose;
public AudioSource Win;

bool winwait = false;

bool OnGround = true;

// Use this for initialization
void Start () {
    Cursor.lockState = CursorLockMode.Locked;
    rb = GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {
    //forwards accleration
    if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.UpArrow)) {
        speed += 0.5f;
        speed = Mathf.Clamp (speed, 0, 10);
        transform.Translate (0,0, speed * Time.deltaTime);
    } else {
        speed -= 0.5f;
        speed = Mathf.Clamp (speed, 0, 10);
        transform.Translate (0,0, speed * Time.deltaTime);
    }
    //backwards accleration
    if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow)) {
        BackSpeed -= 0.5f;
        BackSpeed = Mathf.Clamp (BackSpeed, -5, 0);
        transform.Translate (0,0, BackSpeed * Time.deltaTime);
    } else {
        BackSpeed += 0.5f;
        BackSpeed = Mathf.Clamp (BackSpeed, -5, 0);
        transform.Translate (0,0, BackSpeed * Time.deltaTime);
    }

    //right
    if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) {
        transform.Translate (0.15f,0,0 * Time.deltaTime);
    } 
    //left
    if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) {
        transform.Translate (-0.15f,0,0 * Time.deltaTime * leftSpeed);
    }

    //jump
    if (Input.GetKey (KeyCode.Space) && OnGround == true) {
        Jump ();
        OnGround = false;
    }
    //frontflip
    if (Input.GetKey (KeyCode.Q) && trickwt == false) {
        StartCoroutine (flip ());   
    }
    //backflip
    if (Input.GetKey (KeyCode.E) && trickwt == false) {
        StartCoroutine (Backflip ());
    }
    //corckscrew
    if (Input.GetKey (KeyCode.X) && trickwt == false) {
        StartCoroutine (corckscrew ());
    }

    //pause
    if (Input.GetKey (KeyCode.P) && pause == false) {
        Cursor.lockState = CursorLockMode.None;
        pause = true;
    }else if (Input.GetKey (KeyCode.P) && pause == true) {
        Cursor.lockState = CursorLockMode.Locked;
        pause = false;
    }

    //camera rotation
    float mouseX = Input.GetAxis("Mouse Y");
    float mouseY = Input.GetAxis ("Mouse X");

    float rotAmountX = -mouseX * Mousesens;
    float rotAmountY = mouseY * Mousesens;

    Vector3 targetRot = transform.rotation.eulerAngles;
    Vector3 targetRotI = CameraF.transform.rotation.eulerAngles;

    targetRotI.x += rotAmountX;
    targetRot.y += rotAmountY;

    transform.rotation = Quaternion.Euler (targetRotI);
    CameraF.transform.rotation = Quaternion.Euler (targetRot);


}

void Jump(){
    rb.AddForce (Vector3.up * jump);
    StartCoroutine (Jumpwait ());
}

void flipping(){
    CameraF.transform.Rotate (4.5f, 0, 0);
}


void backflipping(){
    CameraF.transform.Rotate (-4.5f, 0, 0);
}

void corckscrewing(){
    transform.Rotate (-4.5f,0, 9f);
}
//Coroutines
IEnumerator corckscrew(){
    trickwt = true;
    InvokeRepeating ("backflipping", 0, 0.001f);
    yield return new WaitForSeconds (1.3f);
    CancelInvoke ();
    trickwt = false;
}
IEnumerator Backflip(){
    trickwt = true;
    InvokeRepeating ("backflipping", 0, 0.0125f);
    yield return new WaitForSeconds (1f);
    CancelInvoke ();
    trickwt = false;
}

IEnumerator flip(){
    trickwt = true;
    InvokeRepeating ("flipping", 0, 0.0125f);
    yield return new WaitForSeconds (1);
    CancelInvoke ();
    trickwt = false;
}

IEnumerator Jumpwait(){
    jumpwt = true;
    yield return new WaitForSeconds (1.22f);
    jumpwt = false;
}

void OnCollisionEnter(Collision Col){
    if (Col.gameObject.tag == "Destroy Walls") {
        Debug.Log ("you have lost");
        this.gameObject.transform.position = StartPos.transform.position;
        Loose.Play ();
    }
    if (Col.gameObject.tag == "Win!" && winwait == false) {
        //StartPos.transform.position = 
        Debug.Log ("You Win");
        Win.Play ();
        winwait = true;
    }
    if (Col.gameObject.tag == "Ground") {
        OnGround = true;
    }
}

} `

0 个答案:

没有答案