- Home /
Adding Drag/Force to object on a slope/Hill
Hi Everyone,
This is a question that's been bugging me ever since I started using Unity -
Say I have a object, for this we'll say a cube, now that object is sat at the top of a hill. Say if a character bumped into that cube, the object would move. If the object was pushed down the hill, with one push at the top, how would I be able to make the object 'Slide' down the hill as if it was a cube.
I've read examples which tell you how to make a cube move down a hill, however these examples only work by making the cube move in one direction, I want the cube to move with the hill, rolling from side to side over bumps and what not.
How is this achieved?
~ Alex
Answer by Statement · Mar 13, 2011 at 10:45 PM
Add a Rigidbody to it?
Well I am not sure about how your code works. What you want to do sounds like running a physics simulation. You can do this by adding a rigidbody to the object that should move around with physics. It should also have a collider, and the ground/world should also have colliders so it knows what it can bounce against. To move the rigidbody though code you can apply forces to the rigidbody. You'd need to make sure the colliders aren't triggers as well, as they are thought as of being "transparent" for collision response.
If you're having custom movement code in your object with the added rigidbody, then the two of them will probably race against each other controlling the transform of the object. You might want to opt out of all that code if the physics do all what you want. If you want to take control over it as some point of time, you can make it kinematic and use rigidbody.position ins$$anonymous$$d of transform.position. When you want physics to be running again you can unset kinematic behaviour. $$anonymous$$inematic means the code is controlling the movement of the object, not physics interactions.
There is plenty of information in the link I posted if you are keen to learn more, and it contain links to other related topics such as colliders as well. There isn't too much about it but it is surely a bit of a leap if you haven't used physics before. It's fun, it's easy once you get the general idea, and you can create all sorts of nice looking things with physics.
If your object is moving too fast, or it is moving too slow, chances are you're not respecting the size units in unity. 1 unit in unity represents one meter. If your objects are of scale 0.01 or similar, they would appear to move very fast. If your object was large like 100, it would seem like they move very slowly.