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 /
avatar image
1
Question by Stannis42 · Oct 13, 2016 at 07:00 AM · c#controllerjumpjumping

How do I make this jump function work?

Her everyone, I am in a course working with Unity, and we have been given a game that we need to modify. I will be as descriptive as possible and provide pictures. The game is very simple. The player spawns in a room with a countdown and has to shoot boxes. Some give points, some add time, some take away time. They player controller we are given can move around, but not jump. I have been trying to code in a jump mechanic to make a more platformer-styled game, but I really struggle when it comes to code because I am more of a 3D artist. I know this question probably gets asked a lot, but from looking at other people's issues I cannot seem to figure out how to relate what was told to them.

Here is a picture of what the game looks like as it is. alt text

This is the code I am trying to work with. Note that even after adding a rigidbody component, nothing happens. I am not receiving any compiler errors, but the jump function is not working. Any ideas would be great help. Thank you!

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour {
     
     // public variables
     public float moveSpeed = 3.0f;
     public bool grounded = false;
     public float maxSlope = 60;
     public float jumpVelocity = 500;
     private Rigidbody rb;
 
     private CharacterController myController;
 
     // Use this for initialization
 
     void Awake (){
         rb = gameObject.GetComponent<Rigidbody> ();
     
     }
     void Start () {
         // store a reference to the CharacterController component on this gameObject
         // it is much more efficient to use GetComponent() once in Start and store
         // the result rather than continually use etComponent() in the Update function
         myController = gameObject.GetComponent<CharacterController>();
 
     }
     
     // Update is called once per frame
     void Update () {
         // Determine how much should move in the z-direction
         Vector3 movementZ = Input.GetAxis ("Vertical") * Vector3.forward * moveSpeed * Time.deltaTime;
 
         // Determine how much should move in the x-direction
         Vector3 movementX = Input.GetAxis ("Horizontal") * Vector3.right * moveSpeed * Time.deltaTime;
 
         // Convert combined Vector3 from local space to world space based on the position of the current gameobject (player)
         Vector3 movement = transform.TransformDirection (movementZ + movementX);
         
         // Apply gravity (so the object will fall if not grounded)
 
 
         //Debug.Log ("Movement Vector = " + movement);
 
         // Actually move the character controller in the movement direction
         myController.Move (movement);
 
         if (Input.GetKeyDown ("space") && grounded){
             rb.AddForce (0, jumpVelocity, 0);
 
         } 
 
     }
         void OnCollisionStay (Collision col)  {
             foreach (ContactPoint contact in col.contacts)   {
             
             if (Vector3.Angle (contact.normal, Vector3.up) < maxSlope)    {
 
             grounded = true;
                 }  
             }  
         
         } void OnCollisionExit(Collision col)  {
             grounded = false; 
         
         }


 
         
     }
 
 
5cc61ba57d1d72407ccda5814cb1c501.jpg (204.3 kB)
Comment
Add comment · Show 1
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 Orokon · Oct 17, 2016 at 11:11 AM 0
Share

It would be nice if you mark my answer as correct if it solved your issue, so other people who come across this problem can find the answer easily.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Orokon · Oct 13, 2016 at 10:55 AM

@Stannis42 You have to give the AddForce() method a Vector3 like so:

 rb.AddForce(new Vector3(0, jumpVelocity, 0));

Also see the documentation on AddForce: AddForce()

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.

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

Can't get character to Jump on the Y Axis (C#) 2 Answers

Triple Jump C# Script 1 Answer

My Player's Jump and Gravity Speed is not the when how i want it to be 2 Answers

Jumping while moving 1 Answer

Cant Jump ?c# please 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