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 noneuclideangirl · Sep 09, 2016 at 01:34 PM · charactercontrollerjumpingplatformer

CharacterController doesn't jump properly while moving into platform

So I'm making a 3D platformer that uses some point-and-click movement. The idea is, the player object tries to keep moving towards the point that the player clicks, and the player can press a key to jump.

My issue is that when the player presses somewhere on a platform resting on the ground, the player object keeps trying to move into the platform. This wouldn't be an issue, except for some reason the jump doesn't work properly when this is happening. The player object jumps up a bit, then gets kind of stuck and falls back down.

Here's a video of the problem: https://vid.me/cZiL

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMove : MonoBehaviour {
     public float Speed;
     public float JumpSpeed;
     public float Gravity;
     
     private Vector3 destination;
 
     private CharacterController controller;
     private float yspeed;
 
     // Only check for Collision objects
     private int CollisionMask;
 
     void Start() {
         destination = transform.position;
         controller = GetComponent<CharacterController>();
         CollisionMask = 1 << LayerMask.NameToLayer("Collision");
     }
     
     private void SetDestination() {
         // Raycast to find where the user clicked
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit, CollisionMask)) {
             destination = new Vector3(hit.point.x,
                                       0,
                                       hit.point.z);
         }
     }
     
     private void MoveToDestination() {
         // Move only in x and z
         destination.Set(destination.x,
                         transform.position.y,
                         destination.z);
         // If we're more than a given distance away, move closer
         if ((destination - transform.position).sqrMagnitude > Speed * Time.deltaTime) {
             controller.Move((destination - transform.position).normalized * Speed * Time.deltaTime);
         } else {
             transform.position = destination;
         }
     }
 
     private void Jump() {
         yspeed = JumpSpeed;
     }
     
     void Update() {
         if (controller.isGrounded) {
             // Jump if the player presses the appropriate key
             if (Input.GetKeyDown(KeyCode.Z)) {
                 Jump();
             }
         }
         yspeed -= Gravity * Time.deltaTime;
 
         // On left mouse click
         if (Input.GetMouseButtonDown(0)) {
             SetDestination();
         }
         MoveToDestination();
         
         controller.Move(Vector3.up * yspeed * Time.deltaTime);
     }
 }
 




Comment
Add comment · Show 4
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 Sergio7888 · Sep 10, 2016 at 08:12 AM 0
Share

if you use a rigidbody with a collider ins$$anonymous$$d a CharacterController you can use rb.addforce and dont need use complex gravity control formulas.

avatar image noneuclideangirl Sergio7888 · Sep 10, 2016 at 03:06 PM 0
Share

I started the project by using a Rigidbody, but the behaviour didn't feel nice. It felt too bouncey and physics-y, and not tight like I wanted. It still had the same issue.

avatar image Sergio7888 · Sep 10, 2016 at 03:34 PM 0
Share

your y speed is low, you need a value to ensure the ball will reach the necessary heigh before start to fall, not only the exact height.

avatar image noneuclideangirl Sergio7888 · Sep 11, 2016 at 03:26 AM 0
Share

The ball can jump on the platform fine, it's only when the ball is right up against the platform that it has issues. I tried anyway and it didn't help.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with player jumping (Welcome to Unity Answers The best place to ask and answer questions ) 0 Answers

CharacterController glitches if it reaches a ledge while jumping into a wall. 4 Answers

Detecting the instant my character lands vs isGrounded 0 Answers

My charater is not going down ramps properly 0 Answers

How do I script my 2d platformer player controller so that there is a freedom of a few milliseconds when checking if grounded? 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