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 potjam100 · Nov 09, 2019 at 12:27 AM · scripting problem3djumpingcharacter controllercharacter movement

Can't get my character controller to jump properly (3d)

Hi I'm quite new to scripting. I've been looking around but other answers don't solve my problem. I've got my movement down with some help that I got online in a tutorial, but I can't get the player to jump.

My jumps are registering sometimes in the console. It doesn't happen with every key press though. I've been playing around with jump height and gravity but all that does is make a really sudden, high jump for about half a second that sends the player up and down immediately, no matter the settings.

Here's my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ThirdPersonController : MonoBehaviour
 {
     [SerializeField] private float movementSpeed = 5f;
     private float currentSpeed = 8f;
     private float speedSmoothVelocity = 5f;
     private float speedSmoothTime = 5f;
     private float rotationSpeed = 5f;
     public bool isGrounded = true;
     private LayerMask layerMask;
     public float rayLength;
 
     public float gravity = 20f;
     public float jumpHeight = 9f;
 
 
     private Transform mainCameraTransform = null;
 
     private CharacterController controller = null;
     private CharacterAnimation animator;
 
     private void Start()
     {
         controller = GetComponent<CharacterController>();
         animator = GetComponent<CharacterAnimation>();
 
         mainCameraTransform = Camera.main.transform;
     }
 
     private void Update()
     {
         Move();
         RaycastHit();
     }
 
     private void Move()
     {
         Vector2 movementInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
 
         Vector3 forward = mainCameraTransform.forward;
         Vector3 right = mainCameraTransform.right;
 
         forward.y = 0;
         right.y = 0;
 
         forward.Normalize();
         right.Normalize();
 
         Vector3 desiredMoveDirection = (forward * movementInput.y + right * movementInput.x).normalized;
         Vector3 gravityVector = Vector3.zero;
 
         if(!controller.isGrounded)
         {
             gravityVector.y -= gravity;
             if (Input.GetKeyDown("space"))
             {
                 gravityVector.y = jumpHeight;
                 isGrounded = false;
                 Debug.Log("Jump!");
             }
         }
 
         if(desiredMoveDirection != Vector3.zero)
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(desiredMoveDirection), rotationSpeed);
             animator._animRun = true;
         }
 
         else
         {
             animator._animRun = false;
         }
 
         float targetSpeed = movementSpeed * movementInput.magnitude;
         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
 
         controller.Move(desiredMoveDirection * currentSpeed * Time.deltaTime);
         controller.Move(gravityVector * Time.deltaTime);
     }
 
     private void RaycastHit()
     {
         RaycastHit hit;
         Ray controllerRay = new Ray(transform.position, Vector3.down);
         if (Physics.Raycast(controllerRay, out hit, rayLength))
         {
             if (hit.collider.tag == "environment")
             {
                 isGrounded = true;
                 Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * rayLength, Color.yellow);
                 Debug.Log("I hit something!");
             }
         }
     }
 
 
 
 
 }




Any thoughts guys? Help would be greatly appreciated because I'm stuck.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NegiMox · Nov 09, 2019 at 05:30 AM

Wait,as i was reading your script at line 55 you wrote if controller.isGrounded is false meaning he is not touching ground then he should jump if space bar is pressed and below that you wrote (Input.GetKeyDown("space")) this should be (Input.GetKeyDown(KeyCode.Space))

Comment
Add comment · Show 7 · 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 potjam100 · Nov 09, 2019 at 08:52 AM 0
Share

So I just tried these changes but no positive effects have come to light.

Changing the bool and/or the key results in not being able to jump in the slightest. If I change the key press segment to "getbutton/getkey" my character can jump high but will keep rising upwards when holding space.

avatar image lgarczyn potjam100 · Nov 10, 2019 at 12:43 AM 0
Share

Which would imply that your "isGrounded" code, to prevent this, is not working.

avatar image NegiMox potjam100 · Nov 10, 2019 at 07:27 AM 0
Share

The character goes up even if you are in air and you press the key right? this is so bcuz you wrote that if the !controller.isGrounded which mean if character isnt touching the ground that is why you jump when you are in air.

avatar image potjam100 · Nov 10, 2019 at 01:35 PM 0
Share

Okay, makes sense. So I remove the ! to make the code read "if grounded: jump", but what about the actual jump? Changing that segment makes it so that I cant jump at all

avatar image NegiMox potjam100 · Nov 11, 2019 at 04:20 PM 0
Share

You mean if you write if controller.isGrounded then you are not able you jump? The problem here might be of controller.isGrounded function,you should create your detection it is fairly simple you have to just cast a ray from the player. This video will tell you how to do so: https://www.youtube.com/watch?v=j$$anonymous$$u-p_Oy1p$$anonymous$$ or you can search on your own by searching on how to use raycast in unity for jumping and look for ur like this will fix your problem.

avatar image potjam100 · Nov 12, 2019 at 11:22 AM 0
Share

Thanks man but I have a ray cast method at the bottom. I dont think theres a problem with it? I've got a fairly good understanding of ray casts now I think

avatar image NegiMox potjam100 · Nov 14, 2019 at 01:02 PM 0
Share

Good for you.It fine that the ray is pointing downward as it will collide with them so the object would obviously be beneath him thats why.

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

298 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 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 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

Character Controller - Limit controller speed 0 Answers

Jumping with slight air control while retaining velocity 1 Answer

Player keeps flying up 0 Answers

Character's jumping mechanism stuck into something invisible 1 Answer

How to move a character in one direction only when jumping? 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