[英]How do i make enemy move from top to bottom of screen in unity c#?
我正在尝试使用统一性来创建2D空间射击游戏,但我似乎无法使生成的敌人从屏幕的顶部移动到屏幕底部,实际上我对统一性和C#还是陌生的,我可以不知道我的代码有什么问题。
这是我的代码:
using UnityEngine;
using System.Collections;
public class EnemyControl : MonoBehaviour {
float speed;
// Use this for initialization
void Start () {
speed = 2f;
}
// Update is called once per frame
void Update () {
Vector2 position = new transform.position;
position = new Vector2(position.x, position.y - speed * Time.deltaTime);
transform.position = position;
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
if(transform.position.y < min.y) {
Destroy(gameObject);
}
}
这就是您应该如何从上向下移动对象
using UnityEngine;
using System.Collections;
public class EnemyControl : MonoBehaviour {
float speed;
void Start () {
speed = 2.0f;
}
// Update is called once per frame
void FixedUpdate () {
godown();
}
void godown() {
transform.position += Vector3.down *speed* Time.deltaTime;
}
现在在游戏世界中添加另一个对象,您可以将其称为“破坏者”。 将新对象放置在场景的底部。给一个下落的对象一个标记为“ fallingObject”的标签。 并将此脚本与新对象一起添加。
using UnityEngine;
using System.Collections;
public class DestroyCubes : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.name == "fallingObject")
{
Destroy(col.gameObject);
}
}
}
新的脚本名称应为“ DestroyCubes”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.