Having a bit of trouble getting this to work, been trying to do it for a bit now…bear in mind I’m new to Unity.
I’m trying to make it so that when my character is walking down a slope of any kind, the character won’t ‘bounce’ down the slope awkwardly. You can easily see this in the Island Demo, if you walk down a steep slope, the character bounces down it. This makes for unwieldly character control and bugs me. Here I have a picture of the problem.
Once the character moves from position A to B, he is no longer touching the platform, and doesn’t count as ‘grounded’, which means gravity takes over. Since in my game when you’re jumping you lose control over a fair amount of the character’s movements, this is a problem. It also looks ugly.
Most of my movement code is based off of the FPS Walker from the FPS tutorial (mine is a third person game, though)
var speed = 5.0;
var turnSpeed = 0.1;
var jumpSpeed = 8.0;
var gravity = 20.0;
var sensitivity = 3.0;
var dropStep = 10.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate ()
{
if (grounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
moveDirection.y -= hit.distance;
print ("HitDistance" + hit.distance);
print ("MoveDirectY" + moveDirection.y);
}
//moveDirection.y -= gravity * Time.deltaTime;
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
rotationX = 0.0;
rotationX += Input.GetAxis("Mouse X") * sensitivity;
transform.Rotate(Vector3.up, rotationX);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
Screen.showCursor = false;
}
The important part is here
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
moveDirection.y -= hit.distance;
print ("HitDistance" + hit.distance);
print ("MoveDirectY" + moveDirection.y);
}
I define my RaycastHit var as var, then do a raycast. I shoot it downwards from the position of the character, then add the distance it fired before hitting something to the moveDirection Vector3…and it’s not really working. When I walk off a ledge 10 meters high, I should instantly hit the ground, right? The char falls faster than normal, but because I move him the distance of the ray, he should INSTANTLY land on the ground.
Once I get this working, I would of course add in things like only checking when he’s walking off a small ledge, but since I can’t figure this out it’ll have to wait. As I said before, I new to Unity and it’s fairly possible I’m missing something here.
This seems like something most people would have to solve to make any kind of game with slopes, so I hope it’s a common problem…any kind of solution would be nice. Thanks for any help!