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 Vire · Aug 18, 2013 at 12:06 AM · c#charactercontrollerwallclimbing

Wall Climbing Help

Hi,

I am trying to make my character wall climb.. and it works, except that as soon as you let go of the horizontal direction key it will think its no longer colliding with the wall and fall down, I don't want the character to fall off until they either jump or reach the bottom. They shouldn't have to hold the key to stick to the wall. They should be able to stick to it.

Here is my code.

 using UnityEngine;
 using System.Collections;

 public class PlayerMovement : MonoBehaviour
 {
     public float MoveSpeed = 4f;
     public float TurnSpeed = 10f;
     public float JumpSpeed = 10f;
     public float Gravity = 21f;
     public float ClimbSpeed = 10f;
         
     public CharacterController CharacterController;
     
     private Vector3 moveDirection = Vector3.zero;
     private float TerminalVelocity = 0f;
     private float turnRotation = 0f;
     private Quaternion desiredRotation;
     
     private bool Climbing = false;
     
     void Awake()
     {
         CharacterController = GetComponent("CharacterController") as CharacterController;
     }
     
     void Update()
     {
         float deadZone = 0.1f;
         moveDirection = Vector3.zero;
         
         
         //rotate character
         if (Input.GetAxis("Horizontal") > deadZone)
             turnRotation = 0f;
             
         if (Input.GetAxis("Horizontal") < -deadZone)
             turnRotation = 180f;
         
         
         desiredRotation = Quaternion.Euler(0f, turnRotation, 0f);
         transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotation, TurnSpeed * Time.deltaTime);

         
         //move character
         if(Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
         moveDirection.z = Input.GetAxis("Horizontal");
         
         if(moveDirection.magnitude > 1)
             moveDirection.Normalize();
         
         moveDirection *= MoveSpeed;
         
         //jump
         if(CharacterController.isGrounded && Climbing == false)
             TerminalVelocity = 0f;
             if(Input.GetButtonDown("Jump"))
             {
                 TerminalVelocity = JumpSpeed;
             }
         
         //gravity
         if((CharacterController.collisionFlags & CollisionFlags.Above) != 0)
         {
             if(TerminalVelocity > 0)
                 TerminalVelocity = -TerminalVelocity / 2;
         }
         
         TerminalVelocity -= Gravity * Time.deltaTime;
         
         
         //wall climbing
         if((CharacterController.collisionFlags & CollisionFlags.Sides) != 0)
         {
             TerminalVelocity = 0;
             Climbing = true;
             
         }
         else
             Climbing = false;
             
         
         if((Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone) && Climbing == true)
         {
             moveDirection.y = Input.GetAxis("Vertical");
             if(moveDirection.magnitude > 1)
             moveDirection.Normalize();
             
             moveDirection.y *= ClimbSpeed;
         }
         else
             moveDirection.y = TerminalVelocity;
         
         CharacterController.Move(moveDirection * Time.deltaTime);
         
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GeoMaru · Nov 18, 2013 at 10:31 AM

Maybe you should take off the gravity while you climb the wall, so even if you let go of the horizontal direction key, your character will still on the wall, it not exactly what you want, but this is what I can come up with for now.

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 HappyMoo · Jan 21, 2014 at 09:22 AM

Don't make your Climbing directly dependent on Collision. Manage that state yourself. How do you enter the Climbing state? How do you leave it? How does the controls change while you climb? How does the movement change while you climb?

Think about what makes sense, don't let the physics systems moods decide if you climb or not, define it.

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity can't find Character Motor and max forwad speed -1 Answers

How to change CC script to Rigidbody script 1 Answer

jump with character controller best way? 1 Answer


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