c#


Car GameObject falling through the Terrain


I have been working on a project recently and there is a scene where there are mountains, forests and a car.
When the car moves on the terrain, the car penetrates through the terrain
I just want to know how to stop this from happening.
On the Car there is Mesh Collider and RigidBody is attached
On Terrain There is Mesh Collider with Convex to False.
public class Motor : MonoBehaviour {
public float moveSpeed = 5.0f;
public float drag = 0.5f;
public float terminalRoatationSpeed = 25.0f;
public Virtualjoystick moveJoystick;
private Rigidbody controller;
private Transform camTransform;
// Use this for initialization
void Start () {
controller = GetComponent<Rigidbody> ();
controller.maxAngularVelocity = terminalRoatationSpeed;
controller.drag = drag;
camTransform = Camera.main.transform;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 dir = Vector3.zero;
dir.x = Input.GetAxis ("Horizontal");
dir.z = Input.GetAxis ("Vertical");
if(dir.magnitude > 1)dir.Normalize();
if(moveJoystick.InputDirection != Vector3.zero)
{
dir = moveJoystick.InputDirection;
}
// Rotate our Direction vector with Camera
Vector3 rotatedDir = camTransform.TransformDirection(dir);
rotatedDir = new Vector3 (rotatedDir.x, 0, rotatedDir.z);
rotatedDir = rotatedDir.normalized * dir.magnitude;
controller.AddForce (rotatedDir * moveSpeed);
}
}
I've answered this question before but can't find the other answer to mark this as a duplicate. You use WheelCollider for cars.
You need to attach WheelCollider to all the wheels of your car. By doing this your car will always be on top of the Terrain Collider.
WheelCollider.motorTorque is used to move the car forward or backwards.
WheelCollider.brakeTorque is used to brake the car.
WheelCollider.steerAngle is used to steer the car.
There are many tutorials out there and here is one example directly from Unity's doc:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class AxleInfo {
public WheelCollider leftWheel;
public WheelCollider rightWheel;
public bool motor;
public bool steering;
}
public class SimpleCarController : MonoBehaviour {
public List<AxleInfo> axleInfos;
public float maxMotorTorque;
public float maxSteeringAngle;
// finds the corresponding visual wheel
// correctly applies the transform
public void ApplyLocalPositionToVisuals(WheelCollider collider)
{
if (collider.transform.childCount == 0) {
return;
}
Transform visualWheel = collider.transform.GetChild(0);
Vector3 position;
Quaternion rotation;
collider.GetWorldPose(out position, out rotation);
visualWheel.transform.position = position;
visualWheel.transform.rotation = rotation;
}
public void FixedUpdate()
{
float motor = maxMotorTorque * Input.GetAxis("Vertical");
float steering = maxSteeringAngle * Input.GetAxis("Horizontal");
foreach (AxleInfo axleInfo in axleInfos) {
if (axleInfo.steering) {
axleInfo.leftWheel.steerAngle = steering;
axleInfo.rightWheel.steerAngle = steering;
}
if (axleInfo.motor) {
axleInfo.leftWheel.motorTorque = motor;
axleInfo.rightWheel.motorTorque = motor;
}
ApplyLocalPositionToVisuals(axleInfo.leftWheel);
ApplyLocalPositionToVisuals(axleInfo.rightWheel);
}
}
}

Related Links

How can I reuse anonymous properties already computed
Ektron: Getting content by folder ID - get all smartforms in folder?
Static constructor in Singleton design pattern
How to set data-attribues with ASP.NET?
making a .lnk with powershell not working
Checking if List<Tuple<T, T>> is empty
Rename HTML name attribute to get data into model
C# Create constructor for a class from outside of the project
Given a collection of Locations, how can I determine the appropriate Zoom level and Map Center for Bing Maps?
Search and display certain table element
Difference between equals (LINQ) and == (C#) operators?
how to pass file as parameter to your program wpf c#
C# Math Not Accurate?
Sql Cache dependency in Asp.Net
DropDownList model property is empty on post
Are inline expressions compiled on run time or build time?

Categories

HOME
internet-explorer
skype-for-business
javafx
botframework
system-calls
aix
phantomjs
gap-system
jquery-plugins
nltk
itext7
squeak
overrides
kendo-asp.net-mvc
clip-path
ethereum
urllib2
point-of-sale
captcha
fine-uploader
bookmarklet
greensock
smooth-scrolling
airbnb
passbook
dev-c++
klipfolio
infinite-loop
advertising
datastax-enterprise
android-xml
aws-certificate-manager
my.cnf
launchd
typeclass
custom-fields
csproj
folder
slim-lang
log4net-configuration
spring-oauth2
incapsula
repeat
multi-user
azure-storage-files
jquery-inputmask
idea-plugin
mixed-models
uitextview
qt-quick
pexpect
taiga
grails-2.0
html-entities
django-cors-headers
ack
togglebutton
opos
noise
android-vectordrawable
critical-section
overhead
glade
instaparse
git-cherry-pick
hfp
queuing
django-celery
mod
custom-build-step
askbot
biginsights
roslyn-code-analysis
sparkcore
data-representation
waterline
sicstus-prolog
mathics
fedena
frisby.js
tigase
cocos2d-x-2.x
selenium-grid2
disparity-mapping
telerik-appbuilder
lettuce
farpoint-spread
sygic
grape-api
kohana-orm
wpa
extensibility
asp.net-4.0
mosix
symbian3
junction-table
screensharing
fileoutputstream
session-hijacking
qvariant
method-call
quazip
noise-reduction
marathontesting
netbeans7.0
table-valued-parameters
numerical-computing
office-2003
urlscan

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile