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 Musty · Jul 01, 2013 at 03:13 PM · rotationmovementjump

Trying to Add jump to this code.

Hey guys, I got this code from the unity script reference and I've been trying to add Jump function to it, I cannot work out how. I tried using the adforece.up and also tried to use the other move direction from another reference i couldnt work it. Ive also looked everywhere online but cant find a way to do it, can anyone help me with this please? using UnityEngine; using System.Collections;

 public class Player : MonoBehaviour {
     public float speed = 10.0f;
     public float rotationSpeed = 100.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
     void Update() {
         float translation = Input.GetAxis("Vertical") * speed;
         float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
         translation *= Time.deltaTime;
         rotation *= Time.deltaTime;
         transform.Translate(0, 0, translation);
         transform.Rotate(0, rotation, 0);
     }
 }
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 robertbu · Jul 01, 2013 at 03:49 PM 0
Share

Take a look at the sample script for CharacterController.move. The logic is very similar to what you are trying to do, and it implements gravity. You will have to provide your own definition of 'isGrounded'. That may be based off of 'Y' height or you could use a raycast.

1 Reply

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

Answer by InfiniBuzz · Jul 01, 2013 at 03:35 PM

Hi!

I assume that you are not using a character controller ;) you probably did not apply a force, big enough to move the object. Also use ForceMode.Impulse

 public class Player : MonoBehaviour {
     public float speed = 10.0f;
     public float rotationSpeed = 100.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
     public float jumpForce = 10000.0f; // This will be a very high jump ;P
     // or jumpSpeed (see below)
 
     void Update() {
 
        float translation = Input.GetAxis("Vertical") * speed;
        float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
        transform.Translate(0, 0, translation);
        transform.Rotate(0, rotation, 0);
 
        // -
        if(Input.GetKeyDown(KeyCode.Space))
        {
           rigidbody.AddForce(transform.TransformDirection(Vector3.up) * jumpForce, ForceMode.Impulse);
        }
 
    }
 }

you can also set a velocity to the rigidbody:

 rigidbody.velocity += transform.TransformDirection(Vector3.up) * jumpSpeed;

note that you set jumpSpeed to something around 5-10 while jumpForce needs to be a much bigger value.

Comment
Add comment · Show 4 · 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 Musty · Jul 01, 2013 at 03:50 PM 0
Share

Hey! thanks for that, the jump seems to work fine now, the only problem is now after you jump and try to rotate left or right you end you doing a flip and turn up upside down, any idea if there is a solution for that?

avatar image Musty · Jul 01, 2013 at 03:58 PM 0
Share

Never $$anonymous$$d fixed it, have to freeze the x and z. Thanks heaps for your help :)

avatar image InfiniBuzz · Jul 01, 2013 at 03:59 PM 0
Share

ok, as mentioned in robertbu's comment above, you should think about using a charactercontroller if you want to extend this.

avatar image Musty · Jul 01, 2013 at 04:36 PM 0
Share

Yea im going to have to do that anyway, thanks again :)

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

16 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

Related Questions

Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer

Rotation used as movement 4 Answers

Joystick for WASD & Joystick for Camera? 0 Answers

3D Heat Seeking Missile (C#) - Odd problem 1 Answer

How to keep player character on top of a Rolling game object? 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