Is this a bug or am I using transform.up wrong?

I’m creating a Space Shooter in Unity for a school project. The core of the movement is that the ship automatically move forward, but the player can also use a slider to rotate the ship so that it can move freely around a large area. That’s why I tried my hand at transform.up because it’s not directly tied to the y or x axis. I’ve made a very simple script for testing purposes that looks like this:

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

public class transformForward : MonoBehaviour
{
    float speed = 10;
    Rigidbody2D rb;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    private void FixedUpdate()
    {
        rb.velocity = transform.up * speed;
    }
}

I assigned it to an empty GameObject and I can see that when I press play the y property is changing (which is good, it’s supposed to that), but the objects parented to it doesn’t move. Does it only affect the object it’s attached to? Is there another function in Unity 2D that I can use instead? Shouldn’t child objects always move with its parent? I noticed that the child object has a Rigidbody2D component and this is what prevents the child from moving with the parent, but is there anything I can change on that RigidBody component to make it follow the parent?

testPlayer is the empty GameObject and Player is the object attatched to it.


alt text

No you are not using transform.up wrong, transform.up is just a direction that you are using to manipulate your velocity which is fine. The issue is that you are using two separate rigidbody scripts. This generally means you want to simulate two distinct physics objects. When you moving one via its rigidbody velocity, this does not affect the position or velocity of child objects. If, however, you were to move the parent object by directly changing its transform position, the child object WOULD move with it. So you have a few different options:

  1. Just use 1 rigidbody. Unless you are trying to get some kind of ragdoll effect, usually you just want 1 rigidbody per object
  2. Add a physics joint script to your child object. That way when you pull the parent, the joint will pull the child in accordance with phyics
  3. Set you child object to Kinematic instead of dynamic. This will effectively prevent it from being affected by physics, but will still interact with other rigidbodies.
  4. Just use transform.position += transform.up * speed; instead

Noticed that the child moves the opposite direction of the parent and that’s why it stands still on the game screen. It’s like the parent is pushing himself away from the child.

177802-ev0wrkcql3.gif

Are you sure you don’t mean Vector3.up?