2017-10-07 34 views
0

我正在玩团结网站上发现的太空射击游戏教程,我试图让一个老大敌人,当我达到一定数量的点时出现。但我无法弄清楚如何让老板移动到屏幕的中心。我认为这与我为其他敌人制作的先行者剧本有关。其他敌人一直移动到屏幕的底部。我怎样才能让一个只是在屏幕上移动一半的老板移动脚本?统一 - 如何让对象移动然后停止?

public class Mover : MonoBehaviour { 
public float speed; 
private Rigidbody rb; 

void Start() 
{ 
    rb = GetComponent<Rigidbody>(); 
    rb.velocity = transform.forward * speed; 
} 
} 
+1

@Kenneth,您可以接受的答案,如果他们解决你的问题。 – Programmer

+0

好的。谢谢一堆 –

回答

0

可以通过使用下面的停止对象:

rb.velocity = Vector3.zero;

,以测试它,你可以这样做以下:

private void Update() { 
    if (Input.GetKeyDown(KeyCode.S)) { 
     rb.velocity = Vector3.zero; 
    } 
} 
1

我会使用刚体的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); 
} 
相关问题