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 /
This question was closed Apr 24, 2021 at 01:42 PM by Explorix.
avatar image
0
Question by Explorix · Apr 23, 2021 at 04:32 PM · charactercontrollerjump

I aren't able to jump. I find no mistake in the script

Hi guys, I have a little question. I want to make a third person controller. I wrote the script, made a Cinemachine and tried to play the game. I was able to walk, to sprint but i wasn't able to jump more than one time in one game. Can anybody help me? Have I maybe a script mistake Thanks! PS: Sorry for my bad English

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class ThirdPersonController : MonoBehaviour
 {
     public Camera MyCamera;
     public float Speed = 5f;
     public float SprintSpeed = 8f;
     public float RotationSpeed = 15;
     public float JumpSpeed = 5f;
     CharacterController MyController;
 
     float mDesiredRotation = 0f;
     bool mSprinting = false;
 
     float mSpeedY = 0;
     float mGravity = -9.81f;
 
     bool mJumping = false;
     // Start is called before the first frame update
     
     void Start()
     {
         MyController = GetComponent<CharacterController>();
     }
 
     // Update is called once per frame
     void Update()
     {
         float x = Input.GetAxisRaw("Horizontal");
         float z = Input.GetAxisRaw("Vertical");
         if(Input.GetButtonDown("Jump") && !mJumping)
         {
             mJumping = true;
             
             mSpeedY += JumpSpeed;
         }
 
         if(!MyController.isGrounded)
         {
             mSpeedY += mGravity * Time.deltaTime;
         }
         else if(mSpeedY < 0)
         {
             mSpeedY = 0;
         }
         if(mJumping && mSpeedY < 0)
         {
             RaycastHit hit;
             if(Physics.Raycast(transform.position, Vector3.down, out hit, .5f, LayerMask.GetMask("Default")))
             {
                 mJumping = false;
             }
         }
         mSprinting = Input.GetKey(KeyCode.LeftShift);
         Vector3 movement = new Vector3(x, 0, z).normalized;
 
         Vector3 rotatedMovement = Quaternion.Euler(0, MyCamera.transform.rotation.eulerAngles.y, 0) * movement;
         Vector3 verticalMovement = Vector3.up * mSpeedY;
         MyController.Move((verticalMovement + (rotatedMovement * (mSprinting ? SprintSpeed : Speed))) * Time.deltaTime);
 
         if (rotatedMovement.magnitude > 0)
         {
             mDesiredRotation = Mathf.Atan2(rotatedMovement.x, rotatedMovement.z) * Mathf.Rad2Deg;
         }
         Quaternion currentRotation = transform.rotation;
         Quaternion targetRotation = Quaternion.Euler(0, mDesiredRotation, 0);
         transform.rotation = Quaternion.Lerp(currentRotation, targetRotation, RotationSpeed * Time.deltaTime);
 
     }
 }![alt text][1]
 


alt text

alt text

unityscreenshot.jpg (199.0 kB)
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 Captain_Pineapple · Apr 24, 2021 at 09:38 AM 0
Share

are you sure you are pressing the correct button? does your mJumping ever change to true?

avatar image The-Peaceful · Apr 24, 2021 at 11:28 AM 0
Share

Hi, maybe I'm missing out on something, but as far as I understand this, the raycast is to check if you are grounded right? Can't you just use CharacterController.isGrounded to check if the player is grounded when trying to jump? Maybe that works out a bit better than the raycasting :D Also, is your player on the 'Default' layer too? Because that would mean that the raycast also hits the player.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by tannergilliland9 · Apr 23, 2021 at 05:58 PM

Something might be wrong with your raycast on line 47, Can you add Debug.Log("hi") or something and tell us if it gets run?

Comment
Add comment · Show 1 · 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 Explorix · Apr 23, 2021 at 07:23 PM 0
Share

@tannergilliland9 , thanks for the info. I've done it and there was nothing in the console. Have you any idea where is the mistake?

Follow this Question

Answers Answers and Comments

135 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

Related Questions

How to jump multiple times to mid air? 3 Answers

Character doesn't jump sometimes 1 Answer

How to jump using a CharacterController and NavMeshAgent 0 Answers

I cant make my Character jump. 0 Answers

How do you make a characterController jump automatically when it reaches the edge of a platform? 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