C# 我如何使一个物体移动然后停止?
我在unity网站上找到了一个关于太空射击的教程,我在玩这个教程,我想让老板成为我的敌人,当我达到一定的分数时就会出现。但我不知道如何让老板移到屏幕中央。我认为这与我为其他敌人制作的mover脚本有关。其他敌人一直移动到屏幕底部。我如何制作一个boss mover脚本,让它在屏幕上移动一半C# 我如何使一个物体移动然后停止?,c#,unity3d,gameobject,C#,Unity3d,Gameobject,我在unity网站上找到了一个关于太空射击的教程,我在玩这个教程,我想让老板成为我的敌人,当我达到一定的分数时就会出现。但我不知道如何让老板移到屏幕中央。我认为这与我为其他敌人制作的mover脚本有关。其他敌人一直移动到屏幕底部。我如何制作一个boss mover脚本,让它在屏幕上移动一半 public class Mover : MonoBehaviour { public float speed; private Rigidbody rb; void Start() { rb =
public class Mover : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.velocity = transform.forward * speed;
}
}
公共类移动器:单一行为{
公众浮标速度;
私人刚体;
void Start()
{
rb=GetComponent();
rb.velocity=变换向前*速度;
}
}
您可以使用以下命令停止对象:
private void Update() {
if (Input.GetKeyDown(KeyCode.S)) {
rb.velocity = Vector3.zero;
}
}
rb.速度=矢量3.0
要测试它,您可以执行以下操作:
private void Update() {
if (Input.GetKeyDown(KeyCode.S)) {
rb.velocity = Vector3.zero;
}
}
我将使用刚体的
MovePosition
功能,如下所示:
Vector3 screenCenter ;
void Start()
{
rb = GetComponent<Rigidbody>();
screenCenter = Camera.main.ViewportToWorldPoint(new Vector3(0.5,0.5, rb.position.y)); // If I remember right, thte game is a top-down game, right?
}
void FixedUpdate()
{
Vector3 direction = ( screenCenter - rb.position );
float distance = Vector3.Distance( screenCenter, rb.position ) ;
if( distance > Mathf.Epsilon )
rb.MovePosition(rb.position + speed * direction / distance * Time.deltaTime);
}
Vector3屏幕中心;
void Start()
{
rb=GetComponent();
screenCenter=Camera.main.ViewportToWorldPoint(新矢量3(0.5,0.5,rb.position.y));//如果我没记错的话,这个游戏是自顶向下的,对吧?
}
void FixedUpdate()
{
矢量3方向=(屏幕中心-rb位置);
浮动距离=矢量3.距离(屏幕中心,rb.位置);
if(距离>数学ε)
rb.移动位置(rb.位置+速度*方向/距离*时间.增量);
}
@Kenneth,如果他们解决了您的问题,您可以接受答案。好的。非常感谢