- Home /
I can't make my character walk up stairs or slopes using the following code and rigidbody. Can someone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed, jumpSpeed;
public Camera cam;
public Transform camFollow;
public float mousesens;
private float rotX, rotY;
public Rigidbody rb;
public BoxCollider bcol;
public bool grounded = true, stairHit;
public bool isMoving = false, isJumping = false, pressed = false;
public Vector3 movement, slopeMoveDirection;
public RaycastHit slophit;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
if (rb.velocity.x != 0 || rb.velocity.z != 0)
{
isMoving = true;
Debug.Log("moving");
}
else
{
isMoving = false;
Debug.Log("still");
}
//look where player is looking
cam.transform.position = camFollow.position;
rotY += Input.GetAxis("Mouse X") * mousesens;
rotX -= Input.GetAxis("Mouse Y") * mousesens;
rotX = Mathf.Clamp(rotX, -90, 90);
transform.eulerAngles = new Vector3(0, rotY, 0);
cam.transform.eulerAngles = new Vector3(rotX, rotY, 0);
//jumping
grounded = !Physics.BoxCast(bcol.transform.position, bcol.bounds.extents, Vector3.down, bcol.transform.rotation, Mathf.Infinity, LayerMask.GetMask("Ground", "Stairs"));
if (Input.GetButton("Jump") && grounded == true)
{
rb.velocity = new Vector3(0, jumpSpeed, 0);
}
}
void FixedUpdate()
{
//running
if (Input.GetKey(KeyCode.LeftShift) == false)
{
if (Input.GetKey("w"))
{
rb.AddRelativeForce(0, 0, speed);
}
else {
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
if (Input.GetKey("s"))
{
rb.AddRelativeForce(0, 0, -speed);
}
else
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
if (Input.GetKey("d"))
{
rb.AddRelativeForce(speed, 0, 0);
}
else
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
if (Input.GetKey("a"))
{
rb.AddRelativeForce(-speed, 0, 0);
}
else
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
}
else
{
if (Input.GetKey("w"))
{
rb.AddRelativeForce(0, 0, speed * 3);
}
else
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
if (Input.GetKey("s"))
{
rb.AddRelativeForce(0, 0, -speed * 3);
}
else
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
if (Input.GetKey("d"))
{
rb.AddRelativeForce(speed * 3, 0, 0);
}
else
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
if (Input.GetKey("a"))
{
rb.AddRelativeForce(-speed * 3, 0, 0);
}
else
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
}
}
}
I don't know how to solve this problem, I'm here for the same reason, but try using Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") rather than having if statements for every single button for example
void Update()
{
//input
verticalMove = Input.GetAxis("Vertical");
horizontalMove = Input.GetAxis("Horizontal");
//move and rotate
rb.velocity = new Vector3(horizontalMove * speed, 0, verticalMove * speed);
}
Answer by Os-A · Apr 15 at 06:23 AM
For slope: write a method, that check normal direction of mesh under your character and add an additional force if its not equal to Vector3.up. For stairs: increase character controller slope limit or remake your stair's collide mesh.
This is great, but you can completely ignore the character controller and just add another invisible slope above the stairs that the player would walk on
I tried it, the original question was only going to include stairs, but after I tried to use slopes it didn't work so I included slopes to see what people would say.
Answer by nochinator · May 19 at 11:23 AM
If your not using character collider (you aren't) then raycast down below the play to check if they are on ground, when you are not on ground implement your own gravity with addforce function (turn off gravity on the ridged body) when you do make contact with the ground then don't applied that gravity. Finally tweak your raycast distance (the further the steaper angle stairs/slopes you can walk on also as the previous answer stated have the slope above the stairs), drag (I find 3 to be a good starting point), friction (I like none) and bounce (around 0.05 it helps with jumping up blocks, cause at 0 you get stuck on corners) finally don't ask me how you can have drag when there is 0 friction. Hope this helps.
Your answer
Follow this Question
Related Questions
Moving Rigidbody up slopes/stairs 0 Answers
Avoid one gameObject skin in another gameObject 2 Answers
how to Apply Gravity at Run time ..????????? 2 Answers
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer
How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers