- Home /
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
Answer by unity_ek98vnTRplGj8Q · Mar 19, 2021 at 03:40 PM
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:
Just use 1 rigidbody. Unless you are trying to get some kind of ragdoll effect, usually you just want 1 rigidbody per object
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
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.
Just use
transform.position += transform.up * speed;
instead
Thank you for explaining. I don't think I need 2 Rigidbodys, but I was genuinly curious on the effect that was happening. This really helped with understanding the mechanics behind it all. I'm relatively new to Unity and all the 2D movement scripts I've made before manipulates the velocity of an object meaning I have to confine myself to movement along the x and y axis without much inbetween. Thanks again m8, and I'll keep cracking at it.
Also, I'm wondering if there's a good way to make transform.up smoother, like how you multiply the speed with Time.deltaTime. I mean I can do it here with this script by simply changing one line to: rb.velocity = transform.up (speed Time.deltaTime);
but then I have to work with really high number, like changing speed to 1000 to get the same speed I would have gotten without deltaTime at 10.
I'm curious as to why you are having to work with such high numbers -
You want to use Time.deltaTime when you are changing something gradually over a period of time and you want that rate of change to be constant. For instance - if you want something to change its position over a period of time (in other words give it a velocity) then you would say transform.postion += direction * speed * Time.deltaTime
. In this case every 1 second you will move speed units in the desired direction. If you are setting this speed to 1000, then you will be moving 1000 units EVERY SECOND (That's a lot!!).
Be careful though, because saying rigidbody.velocity = direction * speed * Time.deltaTime
is NOT the same thing. If you want an object to move at a constant, smooth velocity then don't use Time.deltaTime here. Your velocity is staying constant, so if you are setting the velocity directly then just set it to whatever value you want it to be and leave it there. rigidbody.velocity = direction * speed
will cause your object to smoothly move speed units every frame, and will result in the same trajectory as transform.position += speed * direction * Time.deltaTime
Hopefully that answered your question in a roundabout way, but if I missed the point please let me know
Yeah no that answers it. I expected it to be 1000 units a second, but it seemed to move at 10, but it was because I was using velocity rather than transform.position. Thank you again for your time and guidance.
Answer by JohnnyCenter · Mar 19, 2021 at 01:56 PM
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.
Your answer
Follow this Question
Related Questions
Handling 2D Slope 1 Answer
Why when set gameobject children he changes the movement behavior 0 Answers
How can i synchronize prefab with NetworkTranform? 1 Answer
AddForce transform.right not working correctly in 2d 1 Answer
In unity3d client “push” an object with a rigidbody2d slower than the host 0 Answers