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 SADOOO · Jan 04, 2017 at 11:30 PM · movementcontrollerjumpbasic

Basic MOVE and JUMP script (Jump trouble)

Hello,

I need help with fixing script, I simply need basic ASDW movement and one hit SPACE jump (forbidden jump while in air) with public floats so I can test it and set the right values. (need to jump around 110cm fast).

I use just Capsule with rigid body, capsule collider.

What I have now work for movement but jump is more like jetpack, it does slowly speed upwards.

I would be glad if somebody would be so kind and helped me with jump issue as I am not programmer, just need this for level design setting/browsing.

Would prefer posting whole functioning code for the same reason.

Thanks in advance.

 using UnityEngine;
 using System.Collections;
  
 public class CharacterController : MonoBehaviour {
     private Rigidbody rg;
     public float speed = 10.0F;
     public float jumpspeed = 10.0F;
    
    
     void Start () {
         Cursor.lockState = CursorLockMode.Locked;
         rg = GetComponent <Rigidbody> ();
     }
    
    
     void Update () {
         float translation = Input.GetAxis ("Vertical") * speed;
         float straffe = Input.GetAxis ("Horizontal") * speed;
         translation *= Time.deltaTime;
         straffe *= Time.deltaTime;
        
         transform.Translate (straffe, 0, translation);
        
         if (Input.GetKey (KeyCode.Space)) {
             Vector3 atas = new Vector3 (0,100,0);
             rg.AddForce(atas * speed);
         }
        
         if (Input.GetKeyDown ("escape"))
             Cursor.lockState = CursorLockMode.None;
        
        
     }
    
 }

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 KoenigX3 · Jan 05, 2017 at 12:12 AM

First of all, you should use Input.GetKeyDown instead of Input.GetKey at the jumping method, since you want to receive only one input. This will get rid of the jetpack-feeling. Also, you could increase the speed value, so you get the right amount of upward movement.

You can use the Physics.Raycast method to check if there is a solid ground under the capsule. Note that it only works, if the ground object has a collider component attached to it.

     // This should be the half of the height of the capsule plus a bit more
     // Or else it will fail to meet the collider of the ground object
     // This default value is ideal for a capsule with a height of 1.0F
     public float raycastLength = 0.52F;
 
     void Update()
     {
         if(Physics.Raycast(transform.position, -transform.up, raycastLength))
         {
             if(Input.GetKeyDown(KeyCode.Space))
             {
                 Vector3 atas = new Vector3 (0,100,0);
                 rg.AddForce(atas * speed);
             }
         }
     }


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 GurkMuppen · Jan 05, 2020 at 12:58 PM

I merged two already existing scripts for movement and jumping. Before you use it you will need a tag on the ground (the object that your player stands on) as "Ground". Here it is:

public class PlayerController : MonoBehaviour {
Rigidbody rb; public bool isGrounded; public float jumpHeight = 5f; public float Speed = 2f;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }
 void OnCollisionEnter(Collision col)
 {
     if (col.gameObject.tag == ("Ground") && isGrounded == false)
     {
         isGrounded = true;
     }
 }
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
     {
         rb.AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
         isGrounded = false;
     }
         
 float moveHorizontal = Input.GetAxis("Horizontal");
 float moveVertical = Input.GetAxis("Vertical");
 rb.AddForce(new Vector3(moveHorizontal, 0.0f, moveVertical) * Speed);                
 }       

}

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

103 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

Related Questions

Problem with Jump script 1 Answer

First person controls for GoogleCardboard 2 Answers

Cant Move towards Right and Jump at same time? 2 Answers

Trouble carrying over scripted gravity to NPC. 0 Answers

Character not jumping smoothly using CharacterController 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