Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Kaizara · Apr 01 at 03:35 AM · rigidbodycolliderslopesstairs

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);
                     }
                 }
 
 
             }
         }
Comment
Add comment · Show 1
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
avatar image nochinator · Apr 20 at 10:13 AM 0
Share

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);
    }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
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
avatar image nochinator · Apr 15 at 09:34 AM 0
Share

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

avatar image Kaizara nochinator · May 19 at 10:06 AM 0
Share

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.

avatar image Kaizara · May 19 at 10:20 AM 0
Share

I am not using a character controller

avatar image
0

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.

Comment
Add comment · Share
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

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

215 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 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

Moving Rigidbody up slopes/stairs 0 Answers

Collision Problem 1 Answer

OnCollisionEnter but have colliding object not move the object it collides with 1 Answer

Detecting when Player stoped to push a rigidbody? 3 Answers

Rigidbody Character Jumps Higher With At Least 2 Collisions 3 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