2015-07-19 138 views
2

所以我有一個非常基本的動畫移動腳本,但我甚至無法獲得實際的動畫部分,因爲我得到一個這樣的錯誤:團結GetComponent導致錯誤

Assets/Player Controllers/PlayerController.cs(18,41): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected 

,直到今天,它是給我一個有關GetComponent的錯誤,但現在我甚至無法複製它,儘管我沒有更改代碼的單行。總之,這裏的全部東西:

using UnityEngine; 
using System.Collections; 

public class PlayerController : MonoBehaviour{ 
public float runSpeed = 6.0F; 
public float jumpHeight = 8.0F; 
public float gravity = 20.0F; 

private Vector3 moveDirection = Vector3.zero; 

void Start(){ 
    controller = GetComponent<CharacterController>(); 
    animController = GetComponent<Animator>(); 
} 

void Update(){ 
    if(controller.isGrounded){ 
     moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 
     moveDirection = transform.TransformDirection(moveDirection); 
     moveDirection *= speed; 

     if(moveDirection = Vector3.zero){//Stopped 
      isWalking = false; 
      isBackpedaling = false; 
     }else if(moveDirection = Vector3.back){//Backpedaling 
      animController.isWalking = false; 
      animController.isBackpedaling = true; 
     }else{//Walking 
      animController.isWalking = true; 
      animController.isBackpedaling = false; 
     } 

     if(Input.GetButton("Jump")){ 
      moveDirection.y = jumpSpeed; 
     } 
    } 
    moveDirection.y -= gravity * Time.deltaTime; 
    controller.Move(moveDirection * Time.deltaTime); 
} 
} 

回答

3

在問候你所得到的錯誤,第18行:

moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 

應該是:

moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 

另一個解決你應該讓(我假設是之前的GetComponent錯誤的來源)是您在Start()方法中分配的變量未聲明。聲明如下:

public class PlayerController : MonoBehaviour{ 
public float runSpeed = 6.0F; 
public float jumpHeight = 8.0F; 
public float gravity = 20.0F; 

private Vector3 moveDirection = Vector3.zero; 

// Added 
private CharacterController controller; 
private Animator animController;