C# 统一-使某物移动,但当它到达某个位置时停止?
这是我的代码:(我是新的,我知道这是否是错误的!)顺便说一句,这是为了统一C# 统一-使某物移动,但当它到达某个位置时停止?,c#,unity3d,C#,Unity3d,这是我的代码:(我是新的,我知道这是否是错误的!)顺便说一句,这是为了统一 void Update () { if (Input.GetButtonDown("Left")) { Transform.position xPrevPos yPrevPos rb.velocity = new Vector2(horzMove, 0) * speed; if(Transform.position == x + 5f) {
void Update () {
if (Input.GetButtonDown("Left"))
{
Transform.position xPrevPos yPrevPos
rb.velocity = new Vector2(horzMove, 0) * speed;
if(Transform.position == x + 5f)
{
## DO SOMETHING ##
}
}
}
所以我想让它做的是首先检查左键是否被按下,然后记住当前位置,然后开始移动,然后如果它到达我想要的位置,它会停止并监听另一个按钮被按下。我之所以写x+5f,是因为我希望它在停止之前向左移动这个距离
希望你能理解 此脚本取自Unity官方网站上的一个教程。这是一个宇宙飞船控制器。您在inspector中设置了一些边界,因此玩家无法将宇宙飞船移动到这些边界之外
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
//public variables you can modify from the inspector
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
//To read the inputs of the player. The Horizontal and Vertical axis
//Are defined in the Editor, Settings Manager, Input Manager.
//You can change that for your own inputs like:
//Input.GetKeyDown(KeyCode.Enter) or key you want to use
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
//Creates a VEctor3D with the inputs and applies the movement
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
//This part keeps the spaceship inside the boundary you passed as parameter
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
//This part is just to make the spaceship tilt to one side or
//another to make it more realistic
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
如果你是一个全新的人,我建议你在开始发展自己的想法之前,开始阅读全部或至少部分教程。这会让你一切都变得容易
以及教程:
编辑:对于您的问题,他们为什么在2D游戏中使用Vector3:
基本上,即使你正在开发一个2D游戏,引擎也有一个三维空间,因此如果你定义了一个向量2,它将被转换成向量3(然后给第三维一个0值)。因此,在本教程中,他们定义了一个向量3,该向量在y坐标中的值为0。此脚本取自Unity官方网站上的一个教程。这是一个宇宙飞船控制器。您在inspector中设置了一些边界,因此玩家无法将宇宙飞船移动到这些边界之外
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
//public variables you can modify from the inspector
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
//To read the inputs of the player. The Horizontal and Vertical axis
//Are defined in the Editor, Settings Manager, Input Manager.
//You can change that for your own inputs like:
//Input.GetKeyDown(KeyCode.Enter) or key you want to use
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
//Creates a VEctor3D with the inputs and applies the movement
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
//This part keeps the spaceship inside the boundary you passed as parameter
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
//This part is just to make the spaceship tilt to one side or
//another to make it more realistic
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
如果你是一个全新的人,我建议你在开始发展自己的想法之前,开始阅读全部或至少部分教程。这会让你一切都变得容易
以及教程:
编辑:对于您的问题,他们为什么在2D游戏中使用Vector3:
基本上,即使你正在开发一个2D游戏,引擎也有一个三维空间,因此如果你定义了一个向量2,它将被转换成向量3(然后给第三维一个0值)。因此,在本教程中,他们定义了一个向量3,该向量在y坐标中的值为0。此代码甚至不编译。请创建一个示例并解释问题所在。(提示:您可能希望存储一个“开始”位置,而不仅仅是前一帧上的位置)我说代码不正确,但我在代码下面的文本字段中描述了我需要什么,您认为2D或3D游戏怎么样?这个动作应该是重复的?就像一个一直从右向左移动的平台,或者更像一个追逐你的敌人,当它到达一个区域或点时会停止?是的,它的2048游戏和它的2d,尽管我想先从1块开始,然后再做其余的,希望helps@IgnacioAlorre我想我能理解,如果你做一个简短的描述,并解释为什么在2d游戏中使用矢量3,我会给你最好的答案。只是简短的评论什么的。这段代码甚至不能编译。请创建一个示例并解释问题所在。(提示:您可能希望存储一个“开始”位置,而不仅仅是前一帧上的位置)我说代码不正确,但我在代码下面的文本字段中描述了我需要什么,您认为2D或3D游戏怎么样?这个动作应该是重复的?就像一个一直从右向左移动的平台,或者更像一个追逐你的敌人,当它到达一个区域或点时会停止?是的,它的2048游戏和它的2d,尽管我想先从1块开始,然后再做其余的,希望helps@IgnacioAlorre我想我能理解,如果你做一个简短的描述,并解释为什么在2d游戏中使用矢量3,我会给你最好的答案。只是简短的评论什么的。谢谢