├── README.md ├── LICENSE └── StairClimb.cs /README.md: -------------------------------------------------------------------------------- 1 | # Rigidbody Step Up Stairs Tutorial 2 | 3 | A simple script from my YouTube Tutorial on stepping up stairs with a Rigidbody similar to how the character controller does. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 DawnsCrowGames 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /StairClimb.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class StairClimb : MonoBehaviour 6 | { 7 | Rigidbody rigidBody; 8 | [SerializeField] GameObject stepRayUpper; 9 | [SerializeField] GameObject stepRayLower; 10 | [SerializeField] float stepHeight = 0.3f; 11 | [SerializeField] float stepSmooth = 2f; 12 | 13 | private void Awake() 14 | { 15 | rigidBody = GetComponent(); 16 | 17 | stepRayUpper.transform.position = new Vector3(stepRayUpper.transform.position.x, stepHeight, stepRayUpper.transform.position.z); 18 | } 19 | 20 | private void FixedUpdate() 21 | { 22 | stepClimb(); 23 | } 24 | 25 | void stepClimb() 26 | { 27 | RaycastHit hitLower; 28 | if (Physics.Raycast(stepRayLower.transform.position, transform.TransformDirection(Vector3.forward), out hitLower, 0.1f)) 29 | { 30 | RaycastHit hitUpper; 31 | if (!Physics.Raycast(stepRayUpper.transform.position, transform.TransformDirection(Vector3.forward), out hitUpper, 0.2f)) 32 | { 33 | rigidBody.position -= new Vector3(0f, -stepSmooth * Time.deltaTime, 0f); 34 | } 35 | } 36 | 37 | RaycastHit hitLower45; 38 | if (Physics.Raycast(stepRayLower.transform.position, transform.TransformDirection(1.5f,0,1), out hitLower45, 0.1f)) 39 | { 40 | 41 | RaycastHit hitUpper45; 42 | if (!Physics.Raycast(stepRayUpper.transform.position, transform.TransformDirection(1.5f,0,1), out hitUpper45, 0.2f)) 43 | { 44 | rigidBody.position -= new Vector3(0f, -stepSmooth * Time.deltaTime, 0f); 45 | } 46 | } 47 | 48 | RaycastHit hitLowerMinus45; 49 | if (Physics.Raycast(stepRayLower.transform.position, transform.TransformDirection(-1.5f,0,1), out hitLowerMinus45, 0.1f)) 50 | { 51 | 52 | RaycastHit hitUpperMinus45; 53 | if (!Physics.Raycast(stepRayUpper.transform.position, transform.TransformDirection(-1.5f,0,1), out hitUpperMinus45, 0.2f)) 54 | { 55 | rigidBody.position -= new Vector3(0f, -stepSmooth * Time.deltaTime, 0f); 56 | } 57 | } 58 | } 59 | } 60 | --------------------------------------------------------------------------------