2017-06-22 52 views
0

我鏈接了我的錯誤代碼的圖片我試過了所有的東西。請有人看看它,讓我知道我做錯了什麼。我試圖做殭屍玩具遊戲[錯誤] [錯誤] [錯誤] [錯誤] [錯誤] [錯誤]殭屍玩具運動腳本不工作

using UnityEngine; 

public class PlayerMovement : MonoBehaviour 
{ 
    public float speed = 6f; 

    Vector3 movement; 
    Animator anim; 
    Rigidbody playerRigidbody; 
    int floorMask; 
    float camRayLength = 100f; 

    void Awake() 
    { 
     floorMask = LayerMask.GetMask("Floor"); 
     anim = GetComponent<Animator>(); 
     playerRigidbody = GetComponent<Rigidbody>(); 
    } 

    void FixedUpdate() 
    { 
     float h = Input.GetAxisRaw("Horizontal"); 
     float v = Input.GetAxisRaw("Vertical"); 

     Move(h, v); 
     Turning(); 
     Animating(h, v); 
    } 

    void Move (float h, float v) 
    { 
     movement.Set(h, 0f, v); 

     movement = movement.normalized * speed * Time.deltaTime; 

     playerRigidbody.MovePosition(transform.position + movement); 
    } 

    void Turning() 
    { 
     Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition); 
     RaycastHit floorHit; 
     if (Physics.Raycast(camRay, out floorHit, CamRayLength, floorMask)) ; 
     { 
      Vector3 playertoMouse = floorHit.point - transform.position; 
      playerToMouse.y = 0f; 

      Quaternion newRotation = Quaternion.LookRotation(playertoMouse); 
      playerRigidbody.MoveRotation(newRotation); 
     } 
    } 
    void Animating (float h, float v) 
    { 
     bool walking = h != 0f || v != 0f; 
     anim.SetBool("IsWalking", walking); 
    } 

回答

0

變量名稱是區分大小寫的C#。

您聲明Vector3變量爲playertoMouse,但隨後您嘗試使用的下一行playerToMouseplayerToMouse.y = 0f;。請注意,To中的T被大寫,而不是聲明的小寫t

我建議你用大寫字母表示t無處不在,使to變得To因爲這是更容易比使其成爲一個小寫t閱讀。

void Turning() 
{ 
    Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition); 
    RaycastHit floorHit; 
    if (Physics.Raycast(camRay, out floorHit, CamRayLength, floorMask)) ; 
    { 
     Vector3 playerToMouse = floorHit.point - transform.position; 
     playerToMouse.y = 0f; 

     Quaternion newRotation = Quaternion.LookRotation(playerToMouse); 
     playerRigidbody.MoveRotation(newRotation); 
    } 
} 
+0

OMG感謝你,所以固定它 –

+0

任何時候......不要忘了[接受](https://meta.stackexchange.com/a/5235)如果您的問題得到解決的答案。 – Programmer