#13 | RIGIDBODY VS. TRANSFORM AND UPDATE VS. FIXEDUPDATE 🎮 | Unity For Beginners | Unity Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 พ.ย. 2024

ความคิดเห็น • 50

  • @derboss66
    @derboss66 9 หลายเดือนก่อน +3

    As a beginner I couldn't really find a source where this was explained properly, until i stumbled upon this video. Thanks!

  • @charlythompson9281
    @charlythompson9281 2 ปีที่แล้ว +7

    Omg, I’m not a beginner (early intermediate), but I’m so glad I decided to give this playlist a go anyhow! You’re including a wealth of background theory that never seems to get covered, and clearing up so many mysteries. This video alone is going to help make decisions about how to approach mechanics so much less stressful. Thank you for doing this, and if you decide to put up an intermediate course on Udemy, I’d be happy to pay $$ for this level of content.

  • @giovannielias8153
    @giovannielias8153 2 ปีที่แล้ว +13

    *_His code, for those who want to just copy and paste:_* 😉
    // Move using RigidBody
    Rigidbody2D _rb;
    float _inputHorizontal;
    float _moveSpeed = 10f;
    Vector2 _currentVelocity;
    // Move using transform
    float moveSpeedTransform = 10f;
    void Start()
    {
    // Move using RigidBody
    _rb = GetComponent();
    }
    void Update()
    {
    // Move using RigidBody
    _inputHorizontal = Input.GetAxisRaw("Horizontal");
    _currentVelocity = new Vector2(_inputHorizontal* _moveSpeed, 0f);
    //MovePlayerTransform();
    }
    void MovePlayerTransform()
    {
    if (Input.GetKey(KeyCode.LeftArrow))
    {
    transform.Translate(new Vector2(-_moveSpeedTransform * Time.deltaTime, 0f), Space.Self);
    }
    else if (Input.GetKey(KeyCode.RightArrow))
    {
    transform.Translate(new Vector2(_moveSpeedTransform * Time.deltaTime, 0f), Space.Self);
    }
    }
    void MovePlayerRigidBody()
    {
    if (_inputHorizontal != 0)
    {
    _rb.velocity = _currentVelocity;
    }
    else
    {
    _currentVelocity = new Vector2(0f, 0f);
    _rb.velocity = _currentVelocity;
    }
    }

  • @RaonCreate
    @RaonCreate 2 ปีที่แล้ว +6

    I’m grateful for this series! Keep making those high quality tuts man, I highly appreciate it.

  • @mikaeltorres
    @mikaeltorres 2 ปีที่แล้ว +1

    No questions now, just wanted to thank you for these lessons! so clear and well done! I have been watching one video per day in the morning before work and learning so much! thakns again!

  • @cmhrkcmn
    @cmhrkcmn 11 หลายเดือนก่อน +1

    thanks bro. i wish i found this channel very much sooner. it would save me from wasting time. thanks again

  • @ChiaPekFann
    @ChiaPekFann 2 ปีที่แล้ว +1

    Thanks for the lessons and sharing, I had been following your channel recently and your explanations are crystal clear and easy to understand. love it!

  • @alisoroush6426
    @alisoroush6426 ปีที่แล้ว

    Really helpful. I got my answers completely by watching this video. thank you so much

  • @doggieshaman
    @doggieshaman ปีที่แล้ว

    Excellent explanation, once again!

  • @bdole100
    @bdole100 ปีที่แล้ว

    Thanks for posting this, it made my day! I'm pretty new to Unity, and I'm trying to make a Candy Crush style game where the game board itself can be manipulated by the player, but would also be altered by physics. You've given me hope that it's not only possible, it's not nearly going to be as complicated to code as I thought!

  • @the-great-mizuti
    @the-great-mizuti ปีที่แล้ว +1

    Thank you! I greatly appreciate that you took the time to think about the YT students learning from your lessons and adjust accordingly to give them the most clarity. So many helpful nuggets of information! You ROCK!!!

  • @512Squared
    @512Squared 2 ปีที่แล้ว

    Cool video!! Thanks Dani

  • @parinamais
    @parinamais ปีที่แล้ว

    very useful, thank you!

  • @georgelezeu5493
    @georgelezeu5493 2 ปีที่แล้ว

    Top tier content.

  • @RGcrasyRG
    @RGcrasyRG 2 ปีที่แล้ว

    Good think to remember this when use cinemachine camera, since it also can update at different time and make your movement jittery.

  • @giakhanhle1524
    @giakhanhle1524 ปีที่แล้ว +1

    Thank you so much!!!!

  • @gxbmb
    @gxbmb 2 ปีที่แล้ว

    thanks for the explanations, it seems to me that if you want the 2 versions of movement to work you could have times where the physics version to take control, then you could just not update using the transform and vise versa, depending on as you say the feeling you going for

  • @ianfenton5988
    @ianfenton5988 7 หลายเดือนก่อน

    Thanks for all this info. It's a great learning tool to get into Unity and very much appreciated. I do have a question though. I'm doing a 2D game as a test where I have a fighter plane. Left and right key changes the fighter direction. To do that I enable or disable the flip X parameter - all works fine... however I also use the shift key for a thruster which makes the fighter plane move extra fast and has a flame (child object) at the rear of the fighter when the shift is pressed. Problem is when I enable flip X the fighter flips but the flame stays in position so when flipped the flame is now at the front of the plane. How can I link (lock) the flame to always be at the rear of the plane?

  • @mimsadsafarpour6592
    @mimsadsafarpour6592 2 ปีที่แล้ว +1

    Hi, I just want to you all know that I was the person who asked the question. Have a nice day.

    • @mimsadsafarpour6592
      @mimsadsafarpour6592 2 ปีที่แล้ว

      By the way, thanks for your great videos I'm learning a lot

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว +1

      I'm glad you did. 🙂 Sometimes I forget things that might confuse people hehe.

  • @shunpeng5700
    @shunpeng5700 2 ปีที่แล้ว

    Can you show us how to detect collision using transform . Specially in custom grid base movement

  • @simsolchannel
    @simsolchannel 2 ปีที่แล้ว

    Hello teacher, I understand a lot about Design Form Signup & Login .. but I want you to make a video lesson about Php MySql Donation fundraissing form, I want to add knowledge on this section .... Finally I hope that you will present and share new knowledge to increase your IT skills ... I would like to express my deep gratitude for your efforts to produce an important lesson and wish you Good health, avoid the deadly Covid-19 disease ..

  • @GodotEnjoyer
    @GodotEnjoyer ปีที่แล้ว

    Shouldn't we multiply the xAxis with speed and then with Time.fixedDeltaTime in case of rigidbody?

  • @galaxy-wy9sd
    @galaxy-wy9sd 2 ปีที่แล้ว +1

    Hey Dani Krossing, I cannot find any information online as to whether editing transforms rightfully belongs in the Update() method. I figured all physics, regardless of any RigidBody component, belonged in FixedUpdate(). Not to doubt your expertise, but I'd prefer some other medium of credibility when it comes to making claims about what Update method the transform belongs in. Is there anything you can link?

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว +1

      Hi! 🙂 I can understand your confusion, so let me answer your question, as well as provide a link.
      Anytime you do something using a RigidBody, it goes into FixedUpdate(). The RigidBody is an actual physics component, which is why it is important to have more control over how frequently it updates, since it is framerate dependent. Meaning if different people play your game using different performing PCs, then the game HAS to perform the same across all devices, no matter their framerates.
      EVERYTHING ELSE that isn't using RigidBody physics goes into Update(). 👈 So if you edit the Transforms directly (not using a RigidBody), it has to go into Update(). Just make sure you remember to include time.DeltaTime whenever you include Transform movement.
      Editing the Transform directly isn't "physics", although I can understand the confusion since you might think... "but it moves, meaning it has to be physics... right?". It's important to understand that editing the Transform, is like moving a object that always wants to stand still. So if you don't tell it to move using code, then it won't move. The RigidBody however, simulates actual physics. So even if you don't tell your object to move, it will still move, since it acts on physics like gravity, friction, air resistance, etc...
      It's also important to note here, that the reason we use both a Update() and a FixedUpdate(), is because FixedUpdate() doesn't get affected by any inefficiencies in our code that is running in Update(). So it will ALWAYS run at fixed intervals.
      That's the difference between RigidBody, and using the Transform. 🙂
      Whenever in doubt, you can always use the official Unity documentation. This link I provided will explain when you want to use FixedUpdate(). As it states... "Use FixedUpdate when using Rigidbody.".
      docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html

    • @galaxy-wy9sd
      @galaxy-wy9sd 2 ปีที่แล้ว

      @@Dani_Krossing Holy shit! Haha, I'm not deserving of a write-up of this size. Seriously, thank you so much for the additional help. Hopefully other viewers surfing the comments will find your reply as insightful as I did.
      This sentence of yours exactly: "EVERYTHING ELSE that isn't using RigidBody physics goes into Update()" needs to be shared everywhere! It isn't clear to new Unity developers what qualifies as "physics."
      Again, thank you!

  • @AstralNostalgia
    @AstralNostalgia ปีที่แล้ว

    Hey there if I am using hte new input system for keyboard - gamepad in my game, and I am using very simple events in unity attached to the new input system... I would like to know, how can I deal with fixedUpdate to seaprate the physics beahvior from the real key-binding ? I ve not gather so many information about this in the new input system. thx

  • @jackv_16
    @jackv_16 2 ปีที่แล้ว

    i have an error with FixedUpdate it said
    Error CS0501 'Player.FixedUpdate()' must declare a body because it is not marked abstract, extern, or partial and i do not know what to do

  • @sanaullahkhan864
    @sanaullahkhan864 2 ปีที่แล้ว

    Sir! can you make a video on how to fix an issue "html is too long" which can be a problem for Google to crawl it?

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      Atm I’m primarily focusing on Game Dev 🙂

  • @BoxOfGod
    @BoxOfGod 2 ปีที่แล้ว

    So RigidBody manipulates Transforms using physics formulas and then you are also trying to move with Translate in same frame which causes erratic movement. Is this reasoning correct?

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      No it’s more in the sense that the transform just updates the position of the object, when the physics is applied. I don’t believe it is the physics that directly move the object through changing the transform.

  • @bertobaggins643
    @bertobaggins643 ปีที่แล้ว

    I'm using RigidBody2D to create a jumping,
    I made a "Jump()" method separately and then in a fixed update I told it to read my jump method. But it doesn't detect inputs most of the time . It works completely fine when in update instead :(
    I'm not sure what I'm doing wrong:
    Rigidbody2D _rb;
    private int _jumpVelocity = 20;
    private void Start()
    {
    _rb = gameObject.GetComponent();
    }
    private void Update()
    {
    Jump();
    }
    private void Jump()
    {
    if(Input.GetKeyDown(KeyCode.Space))
    {
    _rb.velocity = new Vector2(0, _jumpVelocity);
    }
    }

    • @Dani_Krossing
      @Dani_Krossing  ปีที่แล้ว +2

      You always need to check the input in Update, and then put physics based movement in FixedUpdate.
      If you don’t put input in Update, then your jump won’t always be registered. And if you don’t place the jump physics in FixedUpdate, then your physics will be wonky.
      So basically… you need to separate the input check from the physics jump, and put them in each update type 🙂

    • @bertobaggins643
      @bertobaggins643 ปีที่แล้ว

      @@Dani_Krossing thank you very much :D I'll move all my bits about and give it a go 😁👍

  • @maximusthegreatest
    @maximusthegreatest 2 ปีที่แล้ว

    Do you know how to get the ACTUAL position of a rigidbody moving through space?(ex. a bullet)
    If I do rb.position or transform.position in FixedUpate() I get a position that is behind the gameobject in the scene view. So basically FixedUpate() is telling me about the past when I need info on the present to do a distance calculation. Any ideas?

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว +1

      If you want the position of a gameobject, then you use transform.position. And you need to get the position in the Update method, not in FixedUpdate. 🙂
      And if it’s RigidBody.position then it’s in FixedUpdate.
      If you are getting a position behind the game object, then my best guess is that your game object is somehow being placed there… Getting the position either way should get you the correct information.

    • @maximusthegreatest
      @maximusthegreatest 2 ปีที่แล้ว

      @@Dani_Krossing So the gameobject is being moved via AddForce(). My problem is the position it reads off of the RigidBody or just the transform.position in FixedUpate() is BEHIND the actual position of the bullet. So when FixedUpdate() checks the position of the bullet its at point b but FixedUpdate doesnt run fast enough to get an accurate position of how fast the bullet is actually going

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      Have you tried using both RigidBody.position and RigidBody.AddForce() in FixedUpdate?
      If you use transform.position in FixedUpdate, it might not return it properly.

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      Just noticed in your original comment you said you had hehe. To be honest it doesn’t sound like an issue with the “position code”, it sounds like something else is either delaying your position from executing in time, or your game object is actually where it says it is, and something else is going wrong.

  • @koraykahraman9031
    @koraykahraman9031 2 ปีที่แล้ว

    I could move the player. After watching this lecture, I mixed up what I know :(

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว +1

      There are many ways to move things inside a game. 🙂 And there is no “right or wrong answer”. You should only consider what works best for you, and what’s more optimal for the given situation. If it works, it works.

    • @koraykahraman9031
      @koraykahraman9031 2 ปีที่แล้ว

      @@Dani_Krossing I can't be mad at you for listing all the possibilities. I think I need to work harder. But for this I will need more of your tutorial videos :)

  • @melwinsam22
    @melwinsam22 2 ปีที่แล้ว

    sir can u make video on phpmyadmin xampp can u say any contact number i have doubt to ask can u guide me brother

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      Atm I’m not doing any PHP videos.

  • @VinhTran-vl1js
    @VinhTran-vl1js ปีที่แล้ว

    You talk too much and make us so difficult to understand

    • @Dani_Krossing
      @Dani_Krossing  ปีที่แล้ว

      I like to explain things in depth so people understand it properly. 🙂 I'm not a "quick how to" channel. I'm sure you'll be able to find other videos on TH-cam, with less talking.

    • @VinhTran-vl1js
      @VinhTran-vl1js ปีที่แล้ว

      @@Dani_Krossingbut too many information :(