简体   繁体   English

我正在尝试将Java脚本统一转换为csharp脚本,但出现一些错误?

[英]I'm trying to convert java script in unity to csharp script but getting some errors?

The script in csharp after converted: 转换后的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 ();
    }
}

The errors on the lines: 线路上的错误:

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

Error CS1502: The best overloaded method match for 'UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)' has some invalid arguments (CS1502) (Assembly-CSharp) 错误CS1502:“ UnityEngine.Mathf.SmoothDamp(float,float,ref float,float)”的最佳重载方法匹配具有一些无效的参数(CS1502)(Assembly-CSharp)

Error CS1620: Argument 3 must be passed with the 'ref' keyword (CS1620) (Assembly-CSharp) 错误CS1620:参数3必须与'ref'关键字(CS1620)一起传递(Assembly-CSharp)

On the line: 在线上:

desiredPosition = CalculatePosition(mouseY, mouseX, Distance);

Error CS0029: Cannot implicitly convert type 'float' to 'UnityEngine.Vector3' (CS0029) (Assembly-CSharp) 错误CS0029:无法将类型'float'隐式转换为'UnityEngine.Vector3'(CS0029)(Assembly-CSharp)

On the line: 在线上:

return TargetLookAt.position + (rotation * direction);

Error CS0029: Cannot implicitly convert type 'UnityEngine.Vector3' to 'float' (CS0029) (Assembly-CSharp) 错误CS0029:无法将类型'UnityEngine.Vector3'隐式转换为'float'(CS0029)(Assembly-CSharp)

On the line: 在线上:

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

Error CS1502: The best overloaded method match for 'UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)' has some invalid arguments (CS1502) (Assembly-CSharp) 错误CS1502:“ UnityEngine.Mathf.SmoothDamp(float,float,ref float,float)”的最佳重载方法匹配具有一些无效的参数(CS1502)(Assembly-CSharp)

Error CS1620: Argument 3 must be passed with the 'ref' keyword (CS1620) (Assembly-CSharp) 错误CS1620:参数3必须与'ref'关键字(CS1620)一起传递(Assembly-CSharp)

On the line: 在线上:

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

Error CS1502: The best overloaded method match for 'UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)' has some invalid arguments (CS1502) (Assembly-CSharp) 错误CS1502:“ UnityEngine.Mathf.SmoothDamp(float,float,ref float,float)”的最佳重载方法匹配具有一些无效的参数(CS1502)(Assembly-CSharp)

Error CS1620: Argument 3 must be passed with the 'ref' keyword (CS1620) (Assembly-CSharp) 错误CS1620:参数3必须与'ref'关键字(CS1620)一起传递(Assembly-CSharp)

On the line: 在线上:

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

Error CS1502: The best overloaded method match for 'UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)' has some invalid arguments (CS1502) (Assembly-CSharp) 错误CS1502:“ UnityEngine.Mathf.SmoothDamp(float,float,ref float,float)”的最佳重载方法匹配具有一些无效的参数(CS1502)(Assembly-CSharp)

Error CS1620: Argument 3 must be passed with the 'ref' keyword (CS1620) (Assembly-CSharp) 错误CS1620:参数3必须与'ref'关键字(CS1620)一起传递(Assembly-CSharp)

The error literally states: 'argument must be passed with the ref keyword`. 错误的字面意思是:“参数必须与ref关键字一起传递”。 This means you have to add ref to the argument, to mark the parameter as in/out. 这意味着您必须将ref添加到参数中,以将参数标记为in / out。

For example: 例如:

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

For your function CalculatePosition , you simply defined the wrong return type. 对于函数CalculatePosition ,您仅定义了错误的返回类型。 The code inside does not return a float, it returns a Vector3, as is natural for a position. 里面的代码不返回浮点数,而是返回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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的统一 2d 运动脚本中有很多错误 - I'm getting so many errors in my unity 2d movement script 我正在尝试制作ListViewExtensions类,但出现一些错误 - I'm trying to make a ListViewExtensions class but getting some errors Unity-如何从js脚本到csharp脚本获取数据? - Unity - How to get data from a js script to a csharp script? 我正在尝试统一制作一个基本的运动脚本,但错误不断出现 - I'm trying to make a basic movement script in unity, but an error keeps showing up 我正在尝试将一个脚本中的 var 值从 c# 中的另一个统一更改 - I'm trying to change var value in one script from another in c# in unity 从Unity中的Android Plugins文件夹中的csharp脚本类中调用Assets文件夹中的csharp脚本类中的函数 - Calling a function in csharp script class in assets folder from a csharp script class from Android Plugins folder in Unity 当我尝试从 Unity StandardAssets 脚本访问“EnginePower”时出现错误 - I'm getting an error when I try to access `EnginePower` from Unity StandardAssets script 在 CEFSharp (cSharp) 中使用 java 脚本执行 HTML 文件 - Exectute HTML file with java script in CEFSharp (cSharp) Unity,如何使用我创建的Patroll.cs脚本? 遇到错误 - Unity, How do i use the Patroll.cs script i created ? Getting errors 我正在统一制作 FPS 游戏并不断收到这些错误 - I'm making an FPS game in unity and keep getting these errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM