简体   繁体   English

如何告诉 Unity 对象在其父对象的每个轴的方向上向前移动?

[英]How to tell a Unity object to move forward in the direction of each of its parent object's axes?

I've used a simple Unity tutorial to make a Space Invaders game, but I want to adapt it into a different game.我使用了一个简单的 Unity 教程来制作 Space Invaders 游戏,但我想将它改编成不同的游戏。

The tutorial made a right-and-left control of the player ship, but I changed it to rotational control (which took a while because for some reason almost no script correctly confined the rotation to the boundaries I've set).该教程对玩家船进行了左右控制,但我将其更改为旋转控制(这需要一段时间,因为出于某种原因几乎没有脚本正确地将旋转限制在我设置的边界内)。

After scripting the rotation, I wanted the shots to move forward on the screen in the direction of the axis to which it is spawned.在编写旋转脚本后,我希望镜头在屏幕上沿着它产生的轴的方向向前移动。 The axis is of an empty child object inside the ship object, so its own angles are always set to 0.该轴是船对象内的一个空子对象,因此其自身的角度始终设置为 0。

I saw the original function controlling the bullet movement:我看到了控制子弹运动的原始函数:

bullet.position += Vector3.up * speed;

still moves it up the screen regardless of how the bullet is rotated.无论子弹如何旋转,它仍会在屏幕上向上移动。 So I tried:所以我试过:

bullet.position += Vector3.forward * speed;

and saw it moves the bullet into the Z axis.看到它把子弹移动到 Z 轴上。

Basically I'm asking is whether there's a sub-function of Vector3 I'm missing which moves an object according to the direction of its own axis?基本上我要问的是是否有我缺少的 Vector3 的子功能,它根据自己的轴的方向移动对象?

Here are the codes of the two classes:下面是两个类的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShotSpawner : MonoBehaviour
{
    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    private float nextFire;
    // Start is called before the first frame update
    void Start()
    {
    }
    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletController : MonoBehaviour
{
    private Transform bullet;
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        bullet = GetComponent<Transform>();
    }
    void FixedUpdate()
    {
        bullet.position += Vector3.up * speed;
        if (bullet.position.y >= 10)
            Destroy(gameObject);
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Enemy")
        {
            Destroy(other.gameObject);
            Destroy(gameObject);
            PlayerScore.playerScore++;
        }
        else if (other.tag == "Base")
            Destroy(gameObject);

    }
}

I found the simple solution The code required我找到了简单的解决方案所需的代码

transform.up

Rather than而不是

Vector3.up

Transform goes by the objects axis while Vector3 goes by the world space.变换沿对象轴进行,而 Vector3 沿世界空间进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM