2017-07-04 50 views
-1

對於我所在的班級,我試圖打開一扇門,但沒有動畫,它在視覺工作室中播放良好,統一測試門沒有任何作用。我不知道如何做到這一點,所以請我懇求任何幫助。所以我試圖創建一扇門,可以在沒有動畫的情況下打開或滑動

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

public class OpenDoor : MonoBehaviour 
{ 
    float smooth = 2.0f; 
    float DoorOpenAngle = 90.0f; 
    float DoorCloseAngle = 0.0f; 
    bool open; 
    bool enter; 



    void OnTriggerEnter(Collider other) 
    { 
     if (other.gameObject.tag == "Player") 
     { 
      enter = true; 
      print("open"); 
     } 
    } 


    void OnTriggerExit(Collider other) 
    { 
     if (other.gameObject.tag == "Player") 
     { 
      enter = false; 
      print("close"); 
     } 
    } 

    void update() 
    { 
     if (open == true) 
     { 
      print("if(open)"); 
      var target = Quaternion.Euler(0, DoorOpenAngle, 0); 
      transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth); 
     } 

     if (open == false) 
     { 
      print("if(!open)"); 
      var target1 = Quaternion.Euler(0, DoorCloseAngle, 0); 
      transform.localRotation = Quaternion.Slerp(transform.localRotation, target1, Time.deltaTime * smooth); 
     } 

     if (enter == true) 
     { 
      print("if(enter)"); 
      if (Input.GetKeyDown("f")) 
      { 
       open = !open; 
      } 
     } 
    } 
} 
+1

日誌說什麼? –

回答

0

代碼應該工作,即使它不是很安全... 確保兩個播放器和門有對撞機。並且門的碰撞器的IsTrigger選項爲真。

相關問題