2015-07-19 144 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;