Hi All:
I am starting my first Unity first person game. I have an fbx terrain that has a “ramp” to another part of the level. And currently moving the character around using the Translate function. The terrain and player both have mesh colliders attached. Now when the character moves, it is stuck in the same y position. Which I understand why that’s happening, so my question is how can I change the y position based on the terrain. So far I have tried the below code which I intend for it to place the character above the ground; with out luck. Can someone point me the right direction?
transform.Translate(characterLoc * (input.Running() ? walkSpeed * runMultiplier : walkSpeed));
transform.Rotate(characterRot * rotationSpeed);
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit)) {
if (hit.collider.gameObject.name == "ground") {
float distanceToGround = hit.distance;
transform.Translate(new Vector3(transform.position.x, transform.position.y + distanceToGround, transform.position.z));
}
}
else if (Physics.Raycast(transform.position, Vector3.up, out hit)) {
if (hit.collider.gameObject.name == "ground") {
float distanceToGround = hit.distance;
transform.Translate(new Vector3(transform.position.x, transform.position.y + distanceToGround, transform.position.z));
}
}