Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JohnnyCenter · Mar 19, 2021 at 01:48 PM · transformrigidbody2d

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 alt text

2.png (52.0 kB)
1.png (49.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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:

  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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JohnnyCenter · Mar 19, 2021 at 08:10 PM 0
Share

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.

avatar image JohnnyCenter · Mar 19, 2021 at 08:23 PM 0
Share

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.

avatar image unity_ek98vnTRplGj8Q JohnnyCenter · Mar 19, 2021 at 08:51 PM 1
Share

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

avatar image JohnnyCenter unity_ek98vnTRplGj8Q · Mar 20, 2021 at 11:49 AM 0
Share

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.

avatar image
0

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.

alt text


ev0wrkcql3.gif (163.6 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by AbandonedCrypt · Mar 19, 2021 at 02:00 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges