- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此脚本(代码)在 UnityScript 中运行良好,但现在我想将其切换到 C#。这是我到目前为止所得到的。我认为“transform.position”和为我的角色生成“随机位置”时存在问题。可能这很简单,但我是 C# 的初学者,所以请帮助我。
public class AntScript : MonoBehaviour
{
public GameObject ant;
public GameObject scoreText;
public GameObject livesText;
public double walkingSpeed = 0.0f;
public int livesNumber = 3;
public int scoreNumber = 0;
float x;
float y;
void Start() { ant = GameObject.Find("Ant");
scoreText = GameObject.Find("Score");
livesText = GameObject.Find("Lives");
//Initialize the GUI components
livesText.GetComponent<Text>().text = "Lives Remaining: " + livesNumber;
scoreText.GetComponent<Text>().text = "Score: " + scoreNumber;
//Place the ant in a random position on start of the game
ant.transform.position.x = generateX();
ant.transform.position.y = generateY();
}
void Update()
{
Vector3 p = transform.position;
if (ant.transform.position.y < -4.35 && livesNumber > 0)
{
livesNumber -= 1;
livesText.GetComponent<Text>().text = "Lives Remaining: " + livesNumber;
generateCoordinates();
}
else if (ant.transform.position.y < -4.35 && livesNumber == 0)
{
Destroy(GameObject.Find("Ant"));
gameOver();
}
else
{
ant.transform.position.y -= walkingSpeed;
}
}
void gameOver()
{
Application.LoadLevel("GameOver");
}
//Generates random x
void generateX()
{
x = Random.Range(-2.54f, 2.54f);
//return x;
}
//Generates random y
void generateY()
{
y = Random.Range(-4.0f, 3.8f);
//return y;
}
//Move the "Ant" to the new coordiantess
void generateCoordinates()
{
//You clicked it!
scoreNumber += 1;
//Update the score display
scoreText.GetComponent<Text>().text = "Score: " + scoreNumber;
ant.transform.position.x = generateX();
ant.transform.position.y = generateY();
}
//If tapped or clicked
void OnMouseDown()
{
//Place the ant at another point
generateCoordinates();
//Increase the walking speed by 0.01
walkingSpeed += 0.01f;
}
}
最佳答案
这确实是C#的一个怪癖,一开始我花了一段时间才弄明白
问题是,transform.position 不是一个字段,它是一个 setter/getter(它内部有一对方法,想象它是 Vector3 GetPosition() 和 SetPosition(Vector3),这意味着你需要传递整个结构在其中,您不能只设置 x 或 y(因为在您拥有所有参数之前无法调用该方法。幸运的是,解决方法非常简单
Vector3 temp = ant.transform.position; // use a getter
temp.x = generateX(); // modify a struct
temp.y = generateY();
ant.transform.position=temp; // use a setter
关于c# - 如何将我的代码从 UnityScript 切换到 C# (csharp)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254688/
我只有一个基本问题。我正在尝试学习如何在 unityscript 中使用函数,我了解到当您使用类似的东西创建函数时 function Example(text) { print(text);
我不太精通 JavaScript,而且我遇到了一个范围问题,我找不到相关引用: class a { var CommonNameVar; } class b extends a { var C
我正在做一个科学项目,我试图创建一个盲人可以玩的视频游戏,所以声音是玩游戏的媒介,没有图形。我用 UnityScript 编写了游戏代码,并使用 Canvas 元素来帮助我了解我在做什么。 我在游戏中
我正在关注 Unity3d 上的“MoveAround”UnityScript 教程。问题是当教师将脚本拖放到对象上时,他编写的代码工作正常,但是当我这样做时,我收到以下错误消息: Unknown i
我正在使用 Unity 2017.2.0b5。我试图帮助那些问过 question 的人关于 Unityscript 不久前,但我注意到: Assets > 创建 > C# 脚本 存在,但 Asset
在 Unity 中,我似乎无法将此 C# 代码转换为 javascript (unityscript): public Color buttonColor; GetComponent().color
我们正在构建一个 3D 游戏,现在我遇到了一个问题,我需要比较多个标签来激活或停用将玩家送回重生位置的触发器。 这是目前的代码: #pragma strict var spawnPoint : Gam
我的敌人脚本链接到预制件并由我的主脚本实例化。 它以随机顺序杀死敌人(我跳到他们身上,有些没有死,这不是我想要的)。(我想要实现的是当我跳到它的头上并播放死亡动画时,一个敌人会死。因此,从这个敌人脚本
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
我对 UnityScript 很陌生,我需要帮助。我正在为我们统一制作的游戏创建菜单,但我不知道如何从键盘上检索向上/向下箭头键。我想通过向上和向下滚动菜单。所以我想做一些类似的事情:“如果按下了向上
我一直在尝试将 Unity 手册中找到的低通滤波器代码示例实现到我的游戏中,但遇到了一些问题。 以下是 Unity 手册中的原始 JavaScript 示例: var AccelerometerUpd
我来自其他编程语言,对 Unityscript 还很陌生。我试图了解 Unityscript 代码正在做什么。 我找到了这一行 var guiTouchPos : Vector2 = touch.po
是否有可能从实例化它的人那里获取实例化对象的信息? 例如,假设我们有对象 A: Instantiate(objectB, gameObject.transform.position, Quaterni
所以我的场景中有很多正方形(大约 200-300)。它们在移动一点,它们不需要互相遮挡。计算机很难向它们添加 Rigidbody2D。我尝试在每个对象上添加 BoxCollider2D 和 Mesh
我在 UnityScript 中有以下脚本,它在 Unity Editor 中称为 JavaScript 但并不完全相同,尤其是在对象循环方面。 public class UpgradeProfile
当我在 Unity 中单击我的脚本(带有 C# 符号)时,它会打开带有 C# 代码的 MonoDevelop,但我想改为使用 UnityScript 编写代码,我该怎么做呢? 最佳答案 在“项目”选项
我在互联网上找到了我的问题的代码,但我需要它在 C# 中,而不是 UnityScript。我试过了,但我不知道如何解决一些问题。我几乎翻译了所有内容。 原文: #pragma strict var p
这在 UnityScript 中会是什么样子? void playerDataLoaded( List players ) { foreach( GameCenterPlayer p in p
我有一个复杂的函数。我计划向其中发送一个函数。 function ComplexFunction( customFunction : function) { //Complex things
此脚本(代码)在 UnityScript 中运行良好,但现在我想将其切换到 C#。这是我到目前为止所得到的。我认为“transform.position”和为我的角色生成“随机位置”时存在问题。可能这
我是一名优秀的程序员,十分优秀!