I’ve been working on this tank shooter, which many of you have helped me with so far.
However im still struggling with the handling of my tanks. I’ve been tinkering with Mass of the rigidbodies on the tanks, trading that off against the velocity i give them and also trying to use a physic material on the floor they drive on to slow them down. im trying to make them more responsive but am not sure how.
They move around on 2 capsule colliders, should i swap these with wheel colliders and rejig my scripting? or can i work around this somehow?
Your code currently has no special setup for friction. generally in driving code where the sideways friction is the same as the forward friction on the colliders sideways friction should be added in. Another thing you can do is adjust the physics materials on the tank’s colliders so that they have more friction along the tank’s x axis and less along the z axis.
that makes a lot of sense, thanks, but im having trouble implementing it. My tanks are effectively just skidding around based on a box collider they have attached (sorry i was wrong when I said capsules earlier), and ive put a physic material on the tank’s box collider, and tried giving it tons of friction in the direction X, as i show in the pic attached. Not sure what im doing! thoughts?
another prob is that my floor has a box collider… which i put on because the mesh collider didnt work… and putting it on manually failed also… plus its flat so it made no sense to put a mesh on it.
does that have any significance? - a box moving along a box?
ive now only got mesh colliders on each capsule that are the sci fi esque wheels. it sits on them. I try adding the physic material, and it falls through the map… what am i missing?!
Any primitive (box, capsule, sphere) will collide nicely with any other primitive and will collide with mesh colliders. The only things that will not collide with each other are meshes.
Never put rigidbodies on mesh colliders and try not to move mesh colliders unless absolutely necessary!!
What you want to do is use a box collider on both your tank and floor like before and then think hard about the settings on the physics material or listen to what I am about to say:
The normal friction settings (Dynamic Friction, Static Friction) mean how much base friction the object has all over in any direction.
You can make an object have more friction along an arbitrary axis as in a car wheel that doesn’t slip sideways as much. First you must specify the axis of added friction. What you have is an axis that points mostly left but a little off.
Imagine a line starting from (0, 0, 0) and traveling 5000 meters to the right, 5 five meters up and 1 meter forward, that line is your axis. The length of that line is meaningless in this case.
What you want instead is an axis that simply points right (and in this case left too I would assume) that would be (1, 0, 0).
Now you specify how much friction is used when sliding on that axis in Dynamic Friction 2 and Static Friction 2. Try 3 or 5 times as much as your normal friction.
What’s the effect of putting rigidbodies on mesh colliders? I’ve been stupidly doing that (for accuracy in space ship models) and the results have sometimes been unexpected.
Well, it’s not quite that bad. The mesh + rigidbody still reacts appropriately to collisions, but arbitrary forces added with the Rigidbody class sometimes push/pull it in strange ways. I think it probably has something to do with the mesh collider being flat.
Either way, I would consider switching to compound colliders if I knew it would make things easier/perform better in the end.
thanks immensely for your reply and explanation, i have taken your advice and im sure had tried such settings before, because then, as now, nothing is happening. My tanks are box colliders, sitting on a floor which is also a box collider, and I have the settings in the picture attached, and nothing is different.
Marble: If you want collision precision (presumably for bullets) go ahead and put a mesh collider on your ship but make it a trigger, then put a big box around your ship that is on Ignore Raycast. Works well for my spaceships.
Will: I drove the red tank around and it looked like it was only in contact with the ground some of the time. It would sometimes stop completely with no reason. This leads me to believe that it is floating most of the time. Maybe you should just have the tanks float by clamping their y position and y velocity and then calculate the friction yourself. Step one is to get the rigidbody’s velocity in local space meaning (0, 0, 1) means the tank is driving forward at one meter per second and (1, 0, 0) means is sliding to the right at one meter per second. With that you can design the handling anyway you want.
thanks for getting back to me, im not sure about the floating as i havent had it stop dead for no reason before, but i guess thats the point of player testing eh!
In terms of how the tanks used to be set up to how they are now - the difference can be seen by comparing the red tank being shot at to the blue being shot at. The blue tank is still set up as I had designed the originally - every part had a mesh when it came in and i put a large box collider around the whole thing in order to allow it to be hit easier and collect power ups easier.
The red tank - a result of me trying to get physic materials to work properly, is the model with no colliders but a large box, no meshes etc, resultantly, when it gets shot by the blue tank, its doesnt get knocked back the way the blue does. In honesty I dont even know why this is, so knowledge wise im still a way off of writing my own code for friction. I will however have a go at it on monday when im back near a computer with Unity on, as for now i’m off to visit family given some health issues so gotta leave the game for a couple days. Thanks again for all your help, will get back to you once I’ve had a go at writing a script.
Wouldn’t be of much use if it was in world space so I have got my money on local. I think I have tested this but I am not sure. I suppose if it was world you could adjust it every frame with transform.TransformDirection();
so its monday and im considering taking your advice and writing additional parts into my driving script to create friction. However, I’m not sure where to start - mostly how to find or address the sideways velocity, I tried printing rigidbody.velocity, but its not showing me a vector that suggests its what im after - ie. when revving up my tank and letting it skid sideways, its not a high value in the X axis, which is what im expecting, or looking for. Any tips on what i should be addressing?