- Home /
How can i stop the car penetrating into the terrain
simply iam having one car model and try to move this using c# scripts. that code contain gets input from keyboard and move the car....its contains transform.rotate and transform.translate functions only.....but my car model penetrating into the terrain(mountain).i,e whenever the camera Z angle gets negative value the car penetrate into the terrain...could anyone help me to stop the penetration and give me the suggestion for car moving in ups and downs.......Thank u
Answer by ThePersister · Sep 09, 2014 at 10:06 AM
I'll give a shot at answering this question, but if I get it wrong, please add images of your scene and add your code, which would give us better insight of the situation.
One thing you should know is that doing a transform.translate sets the position in smooth lerps. However it can ignore the collision too.
First of all, for collisions you'll need a rigidbody on the Car object. Once you have a rigidbody, try using rigidbody.AddForce(); to move the car about, there are plenty of tutorials on this, so you should be fine :)
So maybe that's enough to solve your problem. But if it isn't, please add some scene images and code so we can give more accurate answers :) Best of luck! ;)
hai persister
my code and snap of project is attached here....am new to unity so please explain me clearly....whenever i tried to move the car its working but when it collide with mountain(i,e terrain ups and downs) it is penetrating.....help me to solve the problem...Thanking you
When you mark a Collider as a "Trigger" it will not collide with the rest of the world as stated in the Unity $$anonymous$$anual:
Triggers
The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts.*
Aka,
Trigger Checked => No Collisions
Trigger UnChecked => Collisions
$$anonymous$$eaning, your car cannot be a Trigger (in your context it's not supposed to act like one, considering it's riding over terrain)
The same goes for your mauntains, terrain, make sure their trigger boxes are unchecked!!
I hope that helps, keep trying, keep asking, and if you succeed, please mark my answer as accepted :)
When you mark a Collider as a "Trigger" it will not collide with the rest of the world as stated in the Unity $$anonymous$$anual:
Triggers
The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts.
Aka,
Trigger Checked => No Collisions
Trigger UnChecked => Collisions
$$anonymous$$eaning, your car cannot be a Trigger (in your context it's not supposed to act like one, considering it's riding over terrain)
The same goes for your mauntains, terrain, make sure their trigger boxes are unchecked!!
I hope that helps, keep trying, keep asking, and if you succeed, please mark my answer as accepted :)
Thank u persist...but still my car rides throw the mountains....please give some coding explanation to stop the car penetrating into the mountains......am expecting some codings from u....
Hey gopics, what I'm trying to say is that your problem is not code related, it's all about that trigger checkbox in the inspector. So, I've gone as far as to make a little example project.
In the project there is:
Some Basic Environment, with a slope to give a "$$anonymous$$ountain"-like effect
A $$anonymous$$amikaze cube that launches itself into the wall, as a basic collision example.
A Player cube, that you can control with WASD, it's very basic, but sufficient for this.
The idea here is that all the collisions work, however, when you turn on the "Trigger Checkbox" on any of the colliders, you'll find that nothing collides with it anymore, it "Phases Through", because it's only a Trigger.
Anyway, I'm sure you'll be able to find out with this little project, I hope you appreciate it ;)
Download it here: https://mega.co.nz/#!LVxlDDIQ!1gJF95R7bck6DArU9SEDgt5LdBe9HTLd0fh2-0fvkNw