• 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 Deep_Thunder · Jun 27, 2017 at 10:04 PM · movementcharactercontrollerjumpingwall jumpsurface normal

How to "push" CharacterController off of a wall's surface normal?

Hey guys. As part of my game here, I'm trying to create a wall jump mechanic (i.e., "push" off the wall, whatever the most accurate terminology should be). The main issue is that I can't figure out how to "force move" the CharacterController's X or Z axis.

When the player jumps and hits a wall, the player should be able to press Space and jump in the wall's opposite direction (off of its surface normal). I'm able to ascend on the Y axis, which is fine, just not the X or Z axis. I assume it should work like something similar to this:

moveVector = hit.normal (times) playerSpeed;

Although that doesn't work.

I'm using the Character Controller physics instead of Rigidbody. This is a script I'm creating from scratch. I also used this guy's tutorial for guidance.

If anyone can help me figure out what the issue is, or any suggestions to be able to jump off the wall in the X or Z axis directions, that would be great. Feel free to ask any questions!

This here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerGravity : MonoBehaviour
 {
     public float verticalVelocity;
     public float gravity;
     public float jumpForce;
     public float playerSpeed;
 
     private CharacterController cc;
     private Vector3 moveVector;
     private Vector3 lastMove;
 
     //private bool runEnabled = true; 
     //private bool strafeEnabled = true;
 
     void Start()
     {
         gravity = 14.0f;
         jumpForce = 5.0f;
         playerSpeed = 5.0f;
         cc = this.GetComponent<CharacterController>();
     }
     
     void Update ()
     {
         if (cc.isGrounded)
         {
             verticalVelocity = -gravity * Time.deltaTime;
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 verticalVelocity = jumpForce;
                 Debug.Log("JUMPED!");
             }
         }
         else
         {
             verticalVelocity -= gravity * Time.deltaTime;
             moveVector = lastMove;
         }
 
         moveVector = Vector3.zero;
         moveVector.x = Input.GetAxis("Horizontal");
         moveVector.z = Input.GetAxis("Vertical");
 
         moveVector *= playerSpeed;
         moveVector.y = verticalVelocity;
         moveVector = transform.TransformDirection(moveVector);
         cc.Move(moveVector * Time.deltaTime);
         lastMove = moveVector;
 
         /*if (runEnabled) { moveVector.x = Input.GetAxis("Horizontal"); }
         else { moveVector.x = 0f; }
         if (strafeEnabled) { moveVector.z = Input.GetAxis("Vertical"); }
         else { moveVector.z = 0f; }*/
     }
 
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if (!cc.isGrounded && hit.normal.y < 0.1f)
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 Debug.DrawRay(hit.point, hit.normal, Color.red, 0.75f);
                 Debug.Log("WALL JUMPED");
                 verticalVelocity = jumpForce;
                 //transform.Translate(hit.normal * playerSpeed);
                 moveVector = hit.normal * playerSpeed;
             }
         }
         else if (cc.isGrounded)
         {
             Debug.Log("Player grounded");
         }
     }
     
 }


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 Cherno · Jun 27, 2017 at 10:33 PM 0
Share

There is or at least was a script that lets you push a CC like a rigidbody. I can't remember if it was on the User wiki or somewhere else.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jun 27, 2017 at 10:37 PM

I've posted an answer to a similar question some time ago: it adds a reaction force to CharacterControllers. Hope it can be adapted to your needs - feel free to ask for help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

101 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

Related Questions

Why can't I properly jump? 2 Answers

How to make your character jump? 1 Answer

Character Controller / Charactor Motor has no inputs? 1 Answer

Can't jump while sprinting 1 Answer

How to keep current velocity while jumping? 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