2014-06-19 35 views
0

我想爲我的Unity遊戲編寫一些水下功能,但我無法將CharacterMotor組件切換到CharacterController組件。這是我的代碼:如何將組件從角色馬達切換到角色控制器?

#pragma strict 

var waterLevel : float; 
var myParticles : ParticleSystem; 
private var isUnderwater : boolean; 
private var normalColor : Color; 
private var underwaterColor : Color; 
private var charcontroller:CharacterController; 

function Start() 
    { 
     normalColor = new Color (0.5f, 0.5f, 0.5f, 0.5f); 
     underwaterColor = new Color (0.22f, 0.65f, 0.77f, 0.5f); 
     charcontroller = GetComponent(CharacterController); 
     myParticles.Stop(); 
     GameObject.Find("Blob Light Projector").GetComponent(Projector).enabled = false; 
    } 

function Update() 
    { 
      if ((transform.position.y < waterLevel) != isUnderwater) 
      { 
       isUnderwater = transform.position.y < waterLevel; 
       if (isUnderwater) SetUnderwater(); 
       if (!isUnderwater) SetNormal(); 
      } 

      if(isUnderwater && Input.GetKey(KeyCode.E)) 
      { 
       constantForce.relativeForce = Vector3(0,-200, 0); 
      } 
      else 
      { 
       constantForce.relativeForce = Vector3(0, 0, 0); 
      } 

      if(isUnderwater && Input.GetKey(KeyCode.Q)) 
      { 
       constantForce.relativeForce = Vector3(0, 200, 0); 
      } 
    } 

function SetNormal() 
     { 
      RenderSettings.fogColor = normalColor; 
      RenderSettings.fogDensity = 0.05f; 
      GameObject.Find("Blob Light Projector").GetComponent(Projector).enabled = false; 
     } 

**function SetUnderwater() 
     { 
      RenderSettings.fogColor = underwaterColor; 
      RenderSettings.fogDensity = 0.08f; 
      charcontroller.Move.gravity = 2; 
      charcontroller.Move.maxFallSpeed = 5; 
      charcontroller.Move.maxForwardSpeed = 4; 
      charcontroller.Move.maxSidewaysSpeed = 4; 
      myParticles.Play(); 
      GameObject.Find("Blob Light Projector").GetComponent(Projector).enabled = true; 
     }** 

粗體代碼是錯誤來自的地方。這裏是我得到的錯誤的快照: enter image description here

任何幫助將非常感激!謝謝!

回答

0

您正試圖在CharacterController.Move上設置屬性,這是一個函數。它並不反映CharacterMotor的「運動」屬性。

查看script referencemanual以查看如何使用CharacterController。具體來說,您將使用簡單的Vector3調用Move,並依靠物理系統來處理其餘的問題。

+0

所以,它會像charcontroller.Move {gravity = 2; }? – glenohumeral13

+0

不,就像我說的那樣,Move是一個函數,只需要一個向量就可以移動它多少。我強烈建議你看一下CharacterController參考,這樣你就明白了你在處理什麼。你會發現根本沒有「重力」的實現,所以像水這樣的重力和橫向阻力......你需要將所有這些自己添加到你的組件中。 – Khakionion