• 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 Jinxorskii · Jul 12, 2015 at 11:56 PM · collisionmovementvector3jump

How to Fix Player Jump Against Wall

In my prototype, the player can run jump at/over buildings in 3D space. However, if the player jumps close to the building's edge, rather than just sliding off to the side, they continue to travel at the same velocity. This is not what I want, they should just slide off the side.

Here's what I mean (top view):

What is happening

alt text

What I want to happen

alt text

Any solutions?

Code(C#):

     public class PlayerMovement : MonoBehaviour
         {
             public float sensitivityX = 15F;
         
             public float forwardSpeed = 12.5f;
             public float sprintSpeed = 25f;
             public float strafeSpeed = 8f;
             public float backwardSpeed = 8f;
             [Range(0, 1)]
             public float gravity = 1f;
             public float maxFallSpeed = 100f;
             public float jumpHeight = 10f;
             public float maxChargeTime = 1f;
             public bool disableInput;
         
             Vector3 velocityVector;
             CharacterController controller;
             float v;
             float h;
             float fSpeed;
             float hSpeed;
             float vSpeed;
             bool jump;
             float jumpTimer;
             bool jumping;
         
         
             void Awake()
             {
                 // Make the rigid body not change rotation
                 if (GetComponent<Rigidbody>())
                     GetComponent<Rigidbody>().freezeRotation = true;
         
                 velocityVector = Vector3.zero;
                 controller = GetComponent<CharacterController>();
         
                 v = 0;
                 h = 0;
                 fSpeed = 0;
                 hSpeed = 0;
                 vSpeed = 0;
                 jumpTimer = 0;
         
                 hang = false;
                 jumping = false;
                 disableInput = false;
             }
         
             void Update()
             {
                 transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
         
                 if (jumping && controller.isGrounded)   //right when we hit the ground
                 {
                     jumping = false;
                 }
             }
         
             void FixedUpdate()
             {
                 GetInput();
         
                 if (!controller.isGrounded)
                 {
                     vSpeed -= gravity * Time.deltaTime;
                 }
                 else
                 {
                     fSpeed = 0;
                     hSpeed = 0;
                     vSpeed = 0;
         
                     if (v > 0)
                         fSpeed = v * forwardSpeed * Time.deltaTime;
                     else if (v < 0)
                         fSpeed = v * backwardSpeed * Time.deltaTime;
                     if (h != 0)
                         hSpeed = h * strafeSpeed * Time.deltaTime;
 
 
                     if (jumpTimer > 0f && jump) //ground jump
                     {
         
                         if (jumpTimer <= 0.125f)
                         {
                             vSpeed = jumpHeight * Time.deltaTime;
                         }
                         else if (jumpTimer > 0.125f)
                         {
                             if (jumpTimer > maxChargeTime)
                                 jumpTimer = maxChargeTime;
         
                             vSpeed = (jumpHeight + (jumpHeight * jumpTimer)) * Time.deltaTime;
                         }
                     }
                 }
         
                 if (vSpeed <= -maxFallSpeed)
                     vSpeed = maxFallSpeed;
         
         
                 Vector3 vector = new Vector3(hSpeed, vSpeed, fSpeed);   //movement vector
         
                 vector = transform.TransformDirection(vector);  //convert to world coordinates
         
                 velocityVector = vector;
         
                 controller.Move(velocityVector);
         
                 if (jump && !controller.isGrounded) //if off the ground and jumped, reset jump timer
                     ResetJumpTimer();
         
         
             }
         
             void GetInput()
             {
                 if (!disableInput)
                 {
                     v = Input.GetAxis("Vertical");
                     h = Input.GetAxis("Horizontal");
         
                     //jump charge
                     if (Input.GetButton("Jump"))
                     {
                         jumpTimer += Time.deltaTime;
                     }
         
                     //jump release
                     if (Input.GetButtonUp("Jump"))
                     {
                         jump = true;
                     }
                 }
             }
         
             void ResetJumpTimer()
             {
                 jumpTimer = 0;
                 jump = false;
                 jumping = true;
             }
         }








Comment
Add comment · Show 2
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 4ntihero · Aug 05, 2015 at 03:21 AM 1
Share

hmmm, maybe you could disable player inputs while they are airborn. That would solve your problem unless you want them to be able to move around and change direction while in the air.

avatar image imilanspinka · Aug 05, 2015 at 01:04 PM 0
Share

4ntihero - how do you jump on an object then?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KdRWaylander · Aug 05, 2015 at 12:19 PM

Hi,

07/12 was a bit ago but who knows ...

When your player touches a wall and is jumping, have the wall set player's velocity to zero, at least in the perpendicular direction of the wall, that should be 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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Keep Horizontal Momentum after Jump 2 Answers

2d Platformer Jumping Not Working 1 Answer

How can I make a physics object jump a given height on collision regardless of current velocity? 1 Answer

My movement function with jump won't work with collider function 1 Answer

Sphere get stuck in floor and fails to jump? 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