2016-07-18 2 views
0

Сценарий в CSharp после конвертации:Я пытаюсь преобразовать java-скрипт в единство в скрипт csharp, но получить некоторые ошибки?

using UnityEngine; 
using System.Collections; 

public class MYCLASSNAME : MonoBehaviour { 


    public Transform TargetLookAt; 

    public float Distance = 5.0f; 
    public float DistanceMin = 3.0f; 
    public float DistanceMax = 10.0f; 

    private float mouseX = 0.0f; 
    private float mouseY = 0.0f; 
    private float startingDistance = 0.0f;  
    private float desiredDistance = 0.0f; 

    public float X_MouseSensitivity = 5.0f; 
    public float Y_MouseSensitivity = 5.0f; 
    public float MouseWheelSensitivity = 5.0f; 
    public float Y_MinLimit = -40.0f; 
    public float Y_MaxLimit = 80.0f; 

    public float DistanceSmooth = 0.05f;  
    private float velocityDistance = 0.0f;  
    private Vector3 desiredPosition = Vector3.zero; 

    public float X_Smooth = 0.05f; 
    public float Y_Smooth = 0.1f; 
    private float velX = 0.0f; 
    private float velY = 0.0f; 
    private float velZ = 0.0f; 
    private Vector3 position = Vector3.zero; 

    CursorLockMode wantedMode;  


    void Start(){ 
     Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax); 
     startingDistance = Distance; 
     Reset(); 
     SetCursorState(); 
     OnGUI(); 

    } 

    void LateUpdate(){ 
     if (TargetLookAt == null) 
      return; 

     HandlePlayerInput(); 

     CalculateDesiredPosition(); 

     UpdatePosition(); 
    } 

    void HandlePlayerInput(){ 
     float deadZone= 0.01f; // mousewheel deadZone 

     //if (Input.GetMouseButton(1)) 
     //{ 
     mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity; 
     mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity; 
     //} 

     // this is where the mouseY is limited - Helper script 
     mouseY = ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit); 

     // get Mouse Wheel Input 
     if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone) 
     { 
      desiredDistance = Mathf.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity), 
       DistanceMin, DistanceMax); 
     } 
    } 

    void CalculateDesiredPosition(){ 
     // Evaluate distance 
     Distance = Mathf.SmoothDamp(Distance, desiredDistance, velocityDistance, DistanceSmooth); 

     // Calculate desired position -> Note : mouse inputs reversed to align to WorldSpace Axis 
     desiredPosition = CalculatePosition(mouseY, mouseX, Distance); 
    } 

    float CalculatePosition (float rotationX , float rotationY , float distance ){ 
     Vector3 direction = new Vector3(0, 0, -distance); 
     Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0); 
     return TargetLookAt.position + (rotation * direction); 
    } 

    void UpdatePosition(){ 
     float posX= Mathf.SmoothDamp(position.x, desiredPosition.x, velX, X_Smooth); 
     float posY= Mathf.SmoothDamp(position.y, desiredPosition.y, velY, Y_Smooth); 
     float posZ= Mathf.SmoothDamp(position.z, desiredPosition.z, velZ, X_Smooth); 
     position = new Vector3(posX, posY, posZ); 

     transform.position = position; 

     transform.LookAt(TargetLookAt); 
    } 

    void Reset(){ 
     mouseX = 0; 
     mouseY = 10; 
     Distance = startingDistance; 
     desiredDistance = Distance; 
    } 

    float ClampAngle (float angle , float min , float max ){ 
     while (angle < -360 || angle > 360) 
     { 
      if (angle < -360) 
       angle += 360; 
      if (angle > 360) 
       angle -= 360; 
     } 

     return Mathf.Clamp(angle, min, max); 
    } 

    // Apply requested cursor state 
    void SetCursorState() 
    { 
     Cursor.lockState = wantedMode; 
     // Hide cursor when locking 
     Cursor.visible = (CursorLockMode.Locked != wantedMode); 
    } 

    void OnGUI() 
    { 
     GUILayout.BeginVertical(); 
     // Release cursor on escape keypress 
     if (Input.GetKeyDown (KeyCode.Escape)) 
      Cursor.lockState = wantedMode = CursorLockMode.None; 

     switch (Cursor.lockState) 
     { 
     case CursorLockMode.None: 
      GUILayout.Label ("Cursor is normal"); 
      if (GUILayout.Button ("Lock cursor")) 
       wantedMode = CursorLockMode.Locked; 
      if (GUILayout.Button ("Confine cursor")) 
       wantedMode = CursorLockMode.Confined; 
      break; 
     case CursorLockMode.Confined: 
      GUILayout.Label ("Cursor is confined"); 
      if (GUILayout.Button ("Lock cursor")) 
       wantedMode = CursorLockMode.Locked; 
      if (GUILayout.Button ("Release cursor")) 
       wantedMode = CursorLockMode.None; 
      break; 
     case CursorLockMode.Locked: 
      GUILayout.Label ("Cursor is locked"); 
      if (GUILayout.Button ("Unlock cursor")) 
       wantedMode = CursorLockMode.None; 
      if (GUILayout.Button ("Confine cursor")) 
       wantedMode = CursorLockMode.Confined; 
      break; 
     } 

     GUILayout.EndVertical(); 

     SetCursorState(); 
    } 
} 

Ошибки на линиях:

Distance = Mathf.SmoothDamp(Distance, desiredDistance, velocityDistance, DistanceSmooth); 

CS1502 Ошибка: Лучший перегружен матч метод «UnityEngine.Mathf.SmoothDamp (с плавающей точкой, float, ref float, float) 'имеет некоторые недопустимые аргументы (CS1502) (Assembly-CSharp)

Ошибка CS1620: Аргумент 3 должен быть прошло с 'реф' ключевое слово (CS1620) (сборочно-Csharp)

На линии: CS0029

desiredPosition = CalculatePosition(mouseY, mouseX, Distance); 

Ошибка: Не удается неявно преобразовать тип 'поплавок' в «UnityEngine. Vector3' (CS0029) (сборочно-CSharp)

на линии:

return TargetLookAt.position + (rotation * direction); 
CS0029

Ошибка: Не удается неявно преобразовать тип 'UnityEngine.Vector3' в 'поплавком' (CS0029) (сборочно-CSharp)

На линии:

float posX= Mathf.SmoothDamp(position.x, desiredPosition.x, velX, X_Smooth); 

Ошибка CS1502: Наилучшее перегруженное соответствие метода для 'UnityEngine.Mathf.SmoothDamp (float, float, ref float, float)' имеет некоторые недопустимые аргументы (CS1502) (Assembly-CSha гр)

Ошибка CS1620: Аргумент 3 должен быть принят с 'реф' ключевое слово (CS1620) (сборочно-CSharp)

На линии: CS1502

float posY= Mathf.SmoothDamp(position.y, desiredPosition.y, velY, Y_Smooth); 

Ошибка : Наилучшее перегруженное соответствие метода для 'UnityEngine.Mathf.SmoothDamp (float, float, ref float, float)' имеет некоторые недопустимые аргументы (CS1502) (Assembly-CSharp)

CS

Ошибка: Аргумент 3 должен быть принят с 'реф' ключевое слово (CS1620) (сборочно-CSharp)

На линии:

float posZ= Mathf.SmoothDamp(position.z, desiredPosition.z, velZ, X_Smooth); 

CS1502 Error: Лучший перегруженный метод соответствует 'UnityEngine.Mathf.SmoothDamp (поплавок, поплавок, исх поплавка, поплавок)»имеет некоторые некорректные аргументы (CS1502) (сборочно-CSharp)

Ошибка CS1620: Аргумент 3 должен быть принят с 'реф' ключевое слово (CS1620) (Assembly- CSharp)

ответ

0

ошибка буквально гласит: «аргумент должен быть принят с реф keyword`. Это означает, что вы должны добавить ref в аргумент, чтобы отметить параметр как in/out.

Например:

Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velocityDistance, DistanceSmooth); 

Для вашей функции CalculatePosition, вы просто определить неправильный тип возвращаемого значения. Код внутри не возвращает float, он возвращает Vector3, как это естественно для позиции.

/* Does not return a float! */ 
Vector3 CalculatePosition(float rotationX, float rotationY, float distance) 
{ 
    Vector3 direction = new Vector3(0, 0, -distance); 
    Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0); 
    return TargetLookAt.position + (rotation * direction); 
} 
Смежные вопросы