- Home /
Question by
nexoduck198 · Oct 13, 2020 at 03:31 AM ·
climbingstairs
Rigidbody Climbing up stairs not working
Hello I'm trying to make an FPS game, and I want to use Rigidbody for my movement it works really well and all, but there's one problem, Climbing upstairs doesn't work properly, It works when you get a head start but if you move slow enough you cant climb up the stairs. I don't really care if it doesn't look natural and also I get stuck on walls when pressing forward in mid-air facing a wall a=I made a cod and watched some tutorials but it doesn't work, It work previously but when I added shooting it stopped working I don't know if this is already answers but some help would be appreciated thx.
[RequireComponent(typeof(Rigidbody))] public class PlayerController : MonoBehaviour {
[Header("Player")]
public float stairsWidth = 1.2f;
[Range(0.1f, 10f)]
public float smoothTime = 5f;
public float maxStepHeight = 1;
public int stairDetail = 15;
public float moveSpeed = 1200;
public float jumpForce = 300;
public float groundDist = .4f;
[Header("Camera")]
public float mouseSensitivity = 100f;
float xRotation;
[Header("Refrences")]
public Transform groundCheck;
public LayerMask ground;
public Camera cam;
Rigidbody rb;
Vector3 smoothVelocity;
bool grounded = false;
bool isFirstCheck = false;
bool canMove = true;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
//Checks if is on the ground
grounded = Physics.OverlapSphere(groundCheck.position, groundDist, ground).Length > 0;
if (grounded && Input.GetButtonDown("Jump"))
{
rb.AddForce(Vector3.up * jumpForce);
}
//moves
Vector2 xMov = new Vector2(Input.GetAxisRaw("Horizontal") * transform.right.x, Input.GetAxisRaw("Horizontal") * transform.right.z);
Vector2 zMov = new Vector2(Input.GetAxisRaw("Vertical") * transform.forward.x, Input.GetAxisRaw("Vertical") * transform.forward.z);
Vector2 velocity = (xMov + zMov).normalized * moveSpeed * Time.deltaTime;
//climb stairs or slopes
isFirstCheck = false;
canMove = true;
for (int i = stairDetail; i >= 1; i--)
{
Collider[] c = Physics.OverlapBox(transform.position + new Vector3(0, i * maxStepHeight / stairDetail - transform.localScale.y, 0), new Vector3(stairsWidth, maxStepHeight / stairDetail / 2, stairsWidth), Quaternion.identity, ground);
if (velocity.x > 1f && velocity.y > 1f)
{
print(c.Length > 0);
if (c.Length > 0)
{
if (i == stairDetail)
isFirstCheck = true;
if (!grounded)
{
canMove = false;
}
if (!isFirstCheck)
{
transform.position += new Vector3(0, i * maxStepHeight / stairDetail, 0);
}
}
}
}
if (canMove)
{
rb.velocity = Vector3.SmoothDamp(rb.velocity, new Vector3(velocity.x, rb.velocity.y, velocity.y), ref smoothVelocity, smoothTime * Time.deltaTime);
}
//Rotates camera
float mouseX = Input.GetAxisRaw("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxisRaw("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
rb.rotation *= Quaternion.Euler(Vector3.up * mouseX);
cam.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
}
Comment