Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dog-Gamer · Apr 11, 2016 at 09:56 PM · programming

Climbing wall Script?

So im making a 3D game and i want it to have it so when ever the player bumps into the wall he will try to climb it but i do not know how to program it. I want to have it in C# and im using the original asset for FPS view. Any help?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ejpj123 · Apr 11, 2016 at 11:13 PM

Try this :

 using UnityEngine;
 using System.Collections;
 
 public class MYCLASSNAME : MonoBehaviour {
 
 
 Transform chController;
 bool  inside = false;
 float heightFactor = 3.2f;
 
 private FPSInputController FPSInput;
 
 void  Start (){
     FPSInput = GetComponent<FPSInputController>();
 }
 
 void  OnTriggerEnter ( Collider Col  ){
     if(Col.gameObject.tag == "Ladder")
     {
         FPSInput.enabled = false;
         inside = !inside;
     }
 }
 
 void  OnTriggerExit ( Collider Col  ){
     if(Col.gameObject.tag == "Ladder")
     {
         FPSInput.enabled = true;
         inside = !inside;
     }
 }
         
 void  Update (){
     if(inside == true && Input.GetKey("w"))
     {
             chController.transform.position += Vector3.up / heightFactor;
     }
 }
 [RPC]
 void  Test (){}
 }

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
avatar image
0

Answer by GEOBA · Apr 11, 2016 at 11:27 PM

You would have to use Physics.CheckSphere or Physics.CheckBox or you can use a trigger to check if the player is near a wall. Then you do a Raycast from the player's position with the player's forward vector to check if the player is facing the wall. If the player is near a wall and is facing the wall then the player can climb the wall. Also don't forget to use a layermask to make sure only climbable object are detected.

Try this:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 [RequireComponent(typeof(CapsuleCollider))]
 public class ClimbScript : MonoBehaviour
 {
     public float WalkSpeed = 5f;
     public float ClimbSpeed = 3f;
     public LayerMask wallMask;
 
     bool climbing;
 
     Vector3 wallPoint;
     Vector3 wallNormal;
 
     Rigidbody body;
     CapsuleCollider coll;
 
     // Use this for initialization
     void Start ()
     {
         body = GetComponent<Rigidbody>();
         coll = GetComponent<CapsuleCollider>();
     }
     
     void FixedUpdate ()
     {
         if (NearWall())
         {
             if (FacingWall())
             {
                 // if player presses the climb button
                 if (Input.GetKeyUp(KeyCode.C))
                 {
                     climbing = !climbing;
                 }
             }
             else
             {
                 climbing = false;
             }
         }
         else
         {
             climbing = false;
         }
 
         if (climbing)
         {
             ClimbWall();
         }
         else
         {
             Walk();
         }
     }
 
     void Walk()
     {
         body.useGravity = true;
 
         var v = Input.GetAxis("Vertical");
         var h = Input.GetAxis("Horizontal");
         var move = transform.forward * v + transform.right * h;
 
         ApplyMove(move, WalkSpeed);
     }
 
     bool NearWall()
     {
         return Physics.CheckSphere(transform.position, 3f, wallMask);
     }
 
     bool FacingWall()
     {
         RaycastHit hit;
         var facingWall = Physics.Raycast(transform.position, transform.forward, out hit, coll.radius + 1f, wallMask);
         wallPoint = hit.point;
         wallNormal = hit.normal;
         return facingWall;
     }
 
     void ClimbWall()
     {
         body.useGravity = false;
 
         GrabWall();
 
         var v = Input.GetAxis("Vertical");
         var h = Input.GetAxis("Horizontal");
         var move = transform.up * v + transform.right * h;
 
         ApplyMove(move, ClimbSpeed);
     }
 
     void GrabWall()
     {
         var newPosition = wallPoint + wallNormal * (coll.radius - 0.1f);
         transform.position = Vector3.Lerp(transform.position, newPosition, 10 * Time.deltaTime);
 
         if (wallNormal == Vector3.zero)
             return;
 
         transform.rotation = Quaternion.LookRotation(-wallNormal);
     }
 
     void ApplyMove(Vector3 move, float speed)
     {
         body.MovePosition(transform.position + move * speed * Time.deltaTime);
     }
 }
Comment
Add comment · Show 1 · 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 Ivoryjw · Nov 08, 2018 at 04:38 AM 0
Share

Thanks for sharing this script. This will go a long ways of helping a person I know.

avatar image
0

Answer by drjayb20 · Jan 02, 2020 at 12:41 PM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class WallClimb : MonoBehaviour { Ray ray; public float range = 1f; public float climbspeed = 5f; public float sticktowallforce = 5f;

 // Start is called before the first frame update
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {
     RaycastHit hit;

     if (Physics.Raycast(transform.position, transform.forward, out hit, range))
     {
         if (hit.transform.tag == "Wall")
         {
             if (Input.GetKey(KeyCode.Space))
             {

                 transform.position += transform.forward * Time.deltaTime * sticktowallforce;
                 transform.position += transform.up * Time.deltaTime * climbspeed;
                
             }
         }
     }
 }

}

Comment
Add comment · Show 1 · 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 MeatChips_ · Mar 18, 2021 at 02:08 PM 0
Share

Hello drjayb20,

I tried using this script in my own game. But it did not work. I did exactly the same you did.

And now i added some gravity to it, and now it works. ( It does not work, how it suppose to be work. But it works :p )

This here is my script i used, i dont know what i did wrong. I added gravity to it, that is the only thing how it works right now

 public float climbSpeed = 5f;
 public float sticktowall = 1f;
 public float range = 2f;
 public void WallClimbing()
 {
     RaycastHit hit;
     if (Physics.Raycast(transform.position, transform.forward, out hit, range))
     {
         if (hit.transform.tag == "WallClimbing")
         {
             gravity = 1f;
             if (Input.GetKey(KeyCode.Space))
             {
                 transform.position += transform.forward * Time.deltaTime * sticktowall;
                 transform.position += transform.up * Time.deltaTime * climbSpeed;
             }
         }
     }
     else
     {
         gravity = -7.81f;
     }
 }

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

63 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

Related Questions

Animation problem? 0 Answers

My player doesn't take fall damage ... please help 0 Answers

How to copy and paste animation events from one clip to another if those clips are included in FBXs? 1 Answer

How can I add an article to a noun in this C# code? 0 Answers

How to use hold/tap interactions in new Input System 0 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