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 /
avatar image
0
Question by Nanashi91 · Sep 28, 2012 at 08:13 AM · charactercontrollerjumpmovements

C# let a character jump

Hello,

I´m trying to implement some basic movements like moving around and jumping but I have already a problem with letting my character jump.

I wrote a 'Move' function in which I let the character jump when I´m pressing the space key. The Code looks like that:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class PlayerMovement : MonoBehaviour {
     
     public float speed = 10.0f;
     public float jumpForce = 10.0f;
     public float gravity = 10.0f;
     public float rotateSpeed = 200;
     
     private Vector3 motion = Vector3.zero;
     
     private CharacterController controller
     {
         get
         {
             return GetComponent<CharacterController>();    
         }
     }
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Move();
         Rotate();
     }
     
     private void Move()
     {
         motion = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         motion *= speed;
         motion = transform.TransformDirection(motion);
         
         controller.Move (motion);
         //controller.SimpleMove(motion);
     
         //jump
         if(Input.GetKeyDown (KeyCode.Space) & controller.isGrounded)
         {
             motion.y = jumpForce;
         }
         
         motion.y -= gravity * Time.deltaTime;
         controller.Move(motion * Time.deltaTime);
         
         
     }
     
     private void Rotate()
     {
         transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime, 0);
         Camera.mainCamera.transform.Rotate(-1 * Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime, 0, 0);
             
     }
     
 }

Unfortuatly the character is not jumping. He is jumping once and after it he is just stuck to the ground and not doing anything. The rest of the movements are working fine. Can anyone tell me what I´m doing wrong?

I also think I chose the gravity abit too high. What do you think?

Thank you in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Nanashi91 · Sep 28, 2012 at 10:08 PM

I got it done. Now I use this method as my "move()"-Method and it is working. I think my mistake was that I called "controller.Move (motion);" in between already and not just at the end. But I´m not sure about that.

Anyway, thank you for trying to help me.

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
avatar image
0

Answer by Sundar · Sep 28, 2012 at 04:10 PM

I have noticed that you have only one "&" in jump 'if statement' you need two "&&" like

 //jump
        if(Input.GetKeyDown (KeyCode.Space) && controller.isGrounded)
        {
          motion.y = jumpForce;
        }

Change and see whether it is working.

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 Nanashi91 · Sep 28, 2012 at 08:54 PM 0
Share

I tried to change the "&" to "&&" but nothing changed. He is not jumping.

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

13 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

Related Questions

Two near-identical scripts cause a cat to jump in different ways - why? 2 Answers

Character controller check ground while falling. 1 Answer

jump with character controller best way? 1 Answer

How do I fix my player jumping code? 2 Answers

Jumping Stopped Working. 2 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