- Home /
Moving on uneven terrain issue
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));
}
}
Answer by rickburgen · Feb 25, 2013 at 09:32 PM
The issue was that I was using two mesh colliders one for the terrain and one for the player. Oppps. Noob mistake I guess.
Answer by MountDoomTeam · Feb 16, 2013 at 10:13 PM
is your character just staying on the same Y with that script?
instead of the line
transform.Translate(new Vector3(transform.position.x, transform.position.y + distanceToGround, transform.position.z));
you can write
transform.position.y = hit.point.y + (height of legs);
I think that would make an object always stuck to the floor under it as if it had 1000000000 gravity:))
Ok, I tried that out but didn't work. The raycast is co$$anonymous$$g back false for both checks, so none of that is even being executed. Guess I should have checked that first, but I'm still unsure how to proceed.
Here is a screen shot of my terrain with the player stuck in the Y axis (the green is the top of the terrain)
Answer by RyanZimmerman87 · Feb 16, 2013 at 11:47 PM
You should be able to get pretty accurate movement just with a simple capsule collider on the character. As long as you are using a Rigidbody with gravity enabled.
This will solve your y-axis floating problem, but you may begin to experience slight sliding effects on the x and y axis if you are on a hill. So you might need to cast a ray downwards and then manually keep the character in that spot unless they use movement keys.
I never used their built in character controller features, because when I first started I was so new at coding that I wanted to learn it from scratch.
But as far as I can tell it takes a lot of extra work to set up realistic movements from scratch. Kind of disappointing considering 99% of games probably require this. Perhaps their character controller features would solve this but I never tried it.
So here is what I noticed as I did some more debugging. The second raycast was being called, but the collider was that character itself. Weird!! Is this because the character has vertical height?
There is no rigidbody or character controller attached, as I am trying to make my own basic controller and there is no need for the physics engine.
Can there be an issue with the way the models were imported?