Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to climb a stair 0 Answers

How could I create an endless stairs effect? 2 Answers

I can't make my character walk up stairs or slopes using the following code and rigidbody. Can someone help? 2 Answers

Distinguish between multiple box colliders? 2 Answers

making a climbable boss in 2d 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges