- Home /
Terrain Slope Bouncing
Hi, I have a very simple character controller movement script and it works fine except when i am supposed to run downhill. The player starts hopping down the slope and i would like it to walk normally. I have looked for a solution and found that it might be raycasting but i cant seem to make i work properly.... The most promising i found is this https://forum.unity.com/threads/player-character-movement-jumps-when-moving-down-the-slopes.497911/ but when i try to incorporate it in my script it doenst do what i want (so basically i don't understand what makes that solution work). Can anyone help?
Here is my script
public class PlayerMovement : MonoBehaviour {
public float Speed = 5.0f;
public float JumpSpeed = 7.0f;
public float TurnSpeed = 3.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void FixedUpdate () {
transform.Rotate(0, Input.GetAxis("Horizontal") * TurnSpeed, 0);
if (controller.isGrounded) {
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= Speed;
if (Input.GetButton("Jump")) {
moveDirection.y = JumpSpeed;
}
} else {
moveDirection.y -= gravity * Time.deltaTime;
}
controller.Move(moveDirection * Time.deltaTime);
}
}
Answer by neighborlee · Mar 27, 2019 at 01:17 PM
I"m far from an expert on this right now, but I wonder yes, it's prob some raycasting neeed as raycasting it used to keep a camera from going past the walls of caves or other objects, which is now a new unity script you can access in new component .
I can't imagine though its not a function of gravity, so maybe its a two part solution. I've delved deep into this myself, so far I've had no need, but now given after much terrain sculpting ( complex world), today I see I need SOME form of adjustment to avoid just what you're talking about- its annoying on some level.
BUT, so far anyway, my character isn't jumping, unless the angle of terrain is pretty darn steep, something in real life you'd also be forced into a 'jump', or rather literally a 'fall' position, tumble hard whatever ;0-
The idea is the raycast as somehow the computer needs you to tell it, HEY this is OK, use walk anim NOT jump, but also temporary , doesn't just setting a high value for slope variable work ?
Also this:
" Answer by robertbu · Aug 07, 2013 at 03:23 PM
Raycasting() is used to detect the slope. If you are going just for the slope of the terrain, then I suggest you use Colider.Raycast() rather than Physics.Raycast(). Sample code:
var hit : RaycastHit; var ray = new Ray(transform.position, Vector3.down); if (someCollider.Raycast(ray, hit)) { var slope = hit.normal; //Adjust character based on normal } This gives you back the normal to the slope:
alt text
Your question was unclear about how you wanted to adjust the rotation of your character. A lot of scripts I see here adjust the object to align with the normal. You can do that by:
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation; "
from https://answers.unity.com/questions/509692/slope-detection.html
Hope this helps- I'm having the same problems ( you may already have fixed yrs given post almost a year old, let me know if you would).
Your answer
Follow this Question
Related Questions
How to Logically Match Ground Slope While Using This Code? 1 Answer
Using Navmesh for Player navigation 1 Answer
Slope Limit on Rigidbody First Person Controller 1 Answer
How to fix player jumping while watching downwards and moving with s key ? 1 Answer
game goes slow and characters fall of through the terrain 1 Answer