0
votes

I've used a simple Unity tutorial to make a Space Invaders game, but I want to adapt it into a different game.

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.

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.

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?

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);

    }
}
1
dont parent the bullet to the ship, as if you move the ship the bullet moves, set the parent null and it ill be a root object with a mind of its own and not subjected to the rotation etc of its former ownerBugFinder
@BugFinder I now think the problem might be that the bullet object is actually not patented by the empty GameObject serving as the spawn point. If I increase the Y axis of the parented spawn object it moves in the direction of its axis, as in its own point of view its axis is still zeroed. But the bullet object is a prefab and it probably reads the axis in its absolute value?TLSO
your game is 2d right? stop using 3d directions.. forward should be forward, if not your game object such as a bullet night be orientated oddly, however, as you instantiated it under shotspawn, that may not have the rotation you expect if its a child of a parent object as the spawn rotation may not be moving. you want the ships roationBugFinder
@BugFinder But it doesn't instantiate as a child object, which I think is why it doesn't move along the rotated axis. How can I tell it to indeed instantiate under the shotspawn object?TLSO
@BugFinder OK, I found the way to instantiate it as a child of the scripted object, but that actually made the shots go upwards and rotated them when turning the ship. But the simple solution was to use transform.up instead of Vector3.up. One goes by the objects axis (which was created in line with the current direction of the empty spawn object) while the other goes by the world space.TLSO

1 Answers

3
votes

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.