【问题标题】:How to convert this Unityscript to C#? [Ignore please] [closed]如何将此 Unityscript 转换为 C#? [请忽略] [关闭]
【发布时间】:2014-09-30 11:22:10
【问题描述】:

这是我在 C# 中的代码有什么问题吗?: 有人可以在这段代码中找到错误吗?

using UnityEngine;
using System.Collections;

public class qwe : MonoBehaviour {

    void  Update (){
        float xP = Input.GetAxis ("Horizontal")*Time.deltaTime * 20; 
        transform.Translate(Vector3 xe = new Vector3(xP,0,0));
        transform.position.x = Mathf.Clamp (transform.position.x, -10, 10);
    }
}

【问题讨论】:

  • 抱歉,Stack Overflow 不是代码转换器。
  • 如果你能告诉我们它给你的错误可能会有所帮助......
  • 一段代码sn-p的截图,真的吗?!
  • 这个问题似乎跑题了,因为它是关于代码转换的。

标签: c# unity3d


【解决方案1】:

使用这个

using UnityEngine;
using System.Collections;

public class qwe : MonoBehaviour {

    void  Update (){
        float xP = Input.GetAxis ("Horizontal")*Time.deltaTime * 20;
        Vector3 xe = new Vector3(xP,0,0);
        transform.Translate(xe);
        float x = Mathf.Clamp (transform.position.x, -10, 10);
        transform.position = new Vector3(x,transform.position.y,transform.position.z);
    }
}

【讨论】:

    【解决方案2】:

    您不能更改 Vector3 的一个值,就像您对 x 值所做的那样。您需要创建一个新的 Vector3 并将其分配给 transform.position

    【讨论】:

      【解决方案3】:

      你不需要“Vector3 xe =” 试试这个:

      float xP = Input.GetAxis ("Horizontal")*Time.deltaTime * 20; 
      transform.Translate(new Vector3(xP,0,0));
      transform.position.x = Mathf.Clamp (transform.position.x, -10, 10);
      

      UPD:哦,Cattwood 是对的——你不能只修改 x。您应该创建新的 Vector3 变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-23
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多