- Home /
The question is answered, right answer was accepted
Keeping a CharacterController Perpindicular to the Ground
I have a hover craft script that uses `CharacterController` to move along the ground, it uses `transform.Rotate()` to turn. This works fine on flat ground, but when i go up a slope or incline the craft is still upright, i want it to be perpendicular to the ground. I have tried using `Quaternion.AngleAxis()` and `Vector3.Angle` along with raycast below the craft. This did not work it didn't put the craft in the right orientation and didn't allow me to steer. Is there a way to get it perpendicular to the ground and still maintain relative steering?
Answer by TheDarkVoid · Nov 23, 2012 at 03:29 AM
Here is my solution:
float curGroundAlignDist = Motor.SlowGroundAlignDist + ((Motor.FastGroundAlignDist - Motor.SlowGroundAlignDist)* Motor.curSpeed/Motor.HighSpeed);
RaycastHit ground;
RaycastHit fwd = new RaycastHit();
Vector3 down = thisTransform.TransformDirection(Vector3.up * -1);
Physics.Raycast(thisTransform.position, down, out ground, Motor.GroundConformDist);
float calcFwdAngle = -1 * Mathf.Rad2Deg * Mathf.Atan(Vector3.Distance(thisTransform.position, fwd.point)/Vector3.Distance(thisTransform.position, ground.point));
if(Physics.Raycast(thisTransform.position, thisTransform.TransformDirection(Vector3.forward), out fwd, curGroundAlignDist))
{
if(fwd.point != Vector3.zero)
Angle = Mathf.Lerp(Angle, calcFwdAngle, Time.deltaTime/2);
}
if(fwd.point == Vector3.zero)
Angle = Mathf.Lerp(Angle, 0, Time.deltaTime/2);
if(ground.point == Vector3.zero && fwd.point == Vector3.zero)
Angle = Mathf.Lerp(Angle, 35, Time.deltaTime/2);
if(Angle < Motor.MaxIncline * -1)
Angle = Motor.MaxIncline * -1;
Then I use that Angle in to modify the Euler of my Character controller, i also moved my steering to the euler.
Answer by Dakcenturi · Oct 07, 2012 at 12:39 AM
You'll have to do a ray cast and then adjust the rotation based off the results. There is a free video series on 3DBuzz.com about third person controllers that would probably help dramatically. I remember seeing them handle slopes so that might help.
I watched the series your talking about, but that wont help me.
Answer by Eric5h5 · Oct 07, 2012 at 12:27 AM
You can't use a CharacterController for that, since it's always upright. A CharacterController is not appropriate for vehicles anyway.
with my current setup i might be able to use a rigidbody. i'll have to play around with it. Although i'm sure it's posible to use a character controller for this, just need to figure out quaterions and how to calculate the rotation.