Search Unity

Question Should I use transform.position or velocity to move objects

Discussion in 'Scripting' started by kader1081, Mar 15, 2023.

  1. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
    Hi I use transfrom.position+= deltaVector*Time.deltaTime but which is better rigidbody velocity or this and why or which situations requires which way.
     
    Lunarboii likes this.
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    1. If you want to have proper physics with walls etc, use velocity. That way you cannot clip through walls
    2. NEVER use transform.position or .rotation on a rigidbody. Instead use RigidBody.MovePosition or .MoveRotation to take physics into account
     
    kader1081 likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    Use the Transform API when physics isn't required.

    Used the Rigidbody API when physics is required.

    There's no strictly better here. Just different requirements.
     
    kader1081 likes this.
  4. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    Like the others said, set transfrom.position is an instant teleportation to a position which is nothing you want when your movement is physically driven.
     
    kader1081 likes this.
  5. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
    What about performance which one is better
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    I don't think it matters much, unless you don't use a rigidbody at all. Then you might be able to pull off better performance with just transform. This does require a lot of work to get up to par, since you need to write your own collisions
     
    DiamondEater, kader1081 and D12294 like this.
  7. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    To set a positon is more performant than use the physics system.
     
    kader1081 likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Stop focusing on better.

    Using MovePosition() is like saying "you go here"

    Using .velocity is like saying "travel at this speed"

    They are different things.

    NOTHING in the Unity API is intentionally bad. It's ALL good for some purpose. The purpose things are best suited to are completely dependent on the context and requirements.

    This is software engineering, not art.
     
    pittmakesgames and DevDunk like this.
  9. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Stop that line of thought right there. Modern hardware is orders of magnitude faster than what you need to run most game concepts. If you happen to programm something so badly that it affects your performance noticably, you will be able to easily track down and fix the problem using Unitys profiler. The rule of thumb here is to not consider performance until you run into a measurable problem - or know what you are doing.

    Performance is an advanced topic. As such it requires a bit of general experience and knowledge about algorithmic and data structures to get right. However, for the overwhelming majority of games you wont have to think about optimizations at all. The problem with thinking about performance is that people, especially beginners, end up wasting time optimizing things that wouldnt have ever become a problem, while usually still not noticing the things they might do which actually cause problems. This makes overall project progress slow and usually causes people to lose interrest and quit eventually, before completing their project - which ironically made the performance irrelevant in the end.
    There is only a handful of topics which require considering optimizations before the get go. Namely procedural voxel generation in realtime, simulations like fluid simulation, RTS or similar game types with truckloads of entities, certain shaders, and similarly computationally taxing scenarios.

    For mostly anything else, focus on code readability and maintainability, as well as getting features done for your project. Anything else is a waste of time. Actually taxing topics dont care about a 5% performance difference either - they require optimizations that make them run orders of magnitudes faster, which is usually handled with different data structures, not micro optimizations.

    All of this is to say: have fun developing. Dont stress performance to much. It's irrelevant for most game concepts and comes naturally with experience! :)
     
    Last edited: Mar 16, 2023
    Sluggy likes this.
  10. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    It's going to be completely determined by your game. For example, for pac-man-type movement, transform.position gives smooth, straight predictable motion and it's easy to perfectly hand-check when things ram into each other. But for a log rolling and bouncing down a hill snagging on things as it goes -- rigidbodies for sure. Make a list of the ways your movement should work as far as bouncing, hitting other stuff, twisting and rolling, how exact it needs to be (galaga aliens in formation?) and so on.

    Rigidbodies are probably easier for beginners in "middle" cases (when you have some easy collisions and bounces), esp if you don't enjoy math and raycasting. But it can take a while to learn the tricks so they don't go through walls, or fall through the ground or wobble or pop too much.
     
    pittmakesgames and kader1081 like this.