Search Unity

Unity Physics vs custom motion scripts

Discussion in 'Physics' started by Danz0r77, Feb 21, 2019.

  1. Danz0r77

    Danz0r77

    Joined:
    Mar 19, 2013
    Posts:
    20
    Kind of looking for a bit of general advice really regarding unity's physics vs controlling motion programatically.

    The game I'm attempting to make at the moment is to be a fairly simple 3rd person flying shooter.
    I've tried using Rigidbodies and using the built in physics but I'm really struggling with this to get a predictable, consistent motion when things collide. For example if the player's ship hits the floor, the rotation can become totally messed up and the direction of the ship following that is totally unpredictable - it isn't really the kind of effect I'm trying to achieve.

    I don't want to use things like gravity or velocity. The player's speed will be constant, and rotation precisely handled by controller input so a lot of the built in physics would be redundant.

    I'd like to have an effect whereby when the ship reaches a certain distance from the floor, or boundary wall, there is a graceful redirection (out of the player's hands) that forces the ship back away from what it is about to collide with. If there are other obstacles, for example, a building, I'd like the player to be able to fly towards it but never touch it, always forced around it or away from it.

    Can the above effect be recreated using the built in physics or is it best to try and manage the motion entirely programatically? I.E Setting the ship's direction away from the obstacle manually and moving the ship in that direction for a period of time. Am I creating a lot of extra work for myself going down that route when the built in physics could do it for me? OR would it actually be harder to try and create the effect I'm after using the built in physics?
     
  2. SolarPetal

    SolarPetal

    Joined:
    Nov 7, 2015
    Posts:
    11
    What you are saying is achievable with Unity's physics system: upon entering the close-to-ground zone, simply execute an AddForce(forwardForce, upForce, 0) scaled with Time.deltaTime or a SmoothDamp to "gracefully" redirect it up to the RigidBody of your plane.

    However, for a flying shooter I would recommend using custom motion scripts. And since you don't want to use gravity or velocity I'd recommend it even more.

    While it will still be achievable using the 3D Physics, you will have to do calculations that you could avoid by simply making you planes kinematic and handling motion within your scripts.

    You can still use the Physics system for various objects or functionalities within your game, but from my experience it is much better and easier to use script motion whenever you want precise and controlled movement from an object.
    (i.e. throwing a projectile: calculating a parabola in a 3D plane is much easier than calculating the trajectory of a projectile involving angle, velocity, gravity etc..)
     
    Danz0r77 and hippocoder like this.
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I do both, it's quite common to use a bit of both. Physics API gives many fantastic highly performant queries and solves a hell of a lot of maths. So you can use that up until where you need fine grained control, and just correct if need be - pretty common!

    In my game absolutely everything is inside the Physics system (Physx) but I set velocities or correct them where I need to for example my character controller is a rigidbody capsule, and I use the rich functionality here for contacts (allocations removed in latest Unity). This allows me to modify the vector of movement on surfaces and set the velocity myself while letting Physx resolve all the nasty sliding collision conditions that pop up.

    For objects that bounce, I didn't like the bounce! so when they impact, I use OnCollision to modify how that feels.

    And so on!

    After all when you get down to it, it's just numbers.
     
    Danz0r77 likes this.
  4. Danz0r77

    Danz0r77

    Joined:
    Mar 19, 2013
    Posts:
    20
    Thanks. Seems like I'm probably going down the right route then for the kind of thing I'm looking for. I need precise control, and I've spent hours trying to get that using Physx but it always gets away from me at some point and I lose the ability to tell what is happening.

    I'll try to use Physx where I can but where I need full control I'll continue with finely tuned scripts.
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah you'll find physx quite useful from colliders to raycasts etc... not just ye olde bouncy box.
     
  6. Danz0r77

    Danz0r77

    Joined:
    Mar 19, 2013
    Posts:
    20
    Raycasts are part of Physx then?

    What exactly is required to use the Physx methods? Does the object just need a Rigid Body?
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Just a collider will suffice. Bear in mind the collider (physics representation) only updates every fixed update. If you aren't using simulations (rigidbodies) then you can ignore this and work as you do normally, taking advantage of robust queries, raycasts etc.... :)

    You can also just disable physics fixed timestep and update it manually in your own update loop for absolute full control, but it's getting deep now and you're getting started so just carry on as you are until you need more.

    Just nothing is one or the other, everything can be used.
     
    Danz0r77 likes this.