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 maggieconnell · May 30, 2014 at 01:20 AM · controllerjumpplatformer

Smoothing out jumping?

Hello friends! I'm working on a simple platformer-type game. I've run into a bit of a problem with the script to make the player jump. He jumps, but it is very jagged (for lack of a better word), he simply teleports upward a few units and then falls down, so it looks very unnatural. I'm stuck, and I can't for the life of me come up with a solution, any help would be much appreciated as I am pretty new at C# and game development in general. Thank you!

 using UnityEngine;
 using System.Collections;
 
 public class MoveScript : MonoBehaviour {
 
     protected Animator animator;
 
     public Vector2 speed = new Vector2(50,50);
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector2 movement;
     private Vector3 moveDirection = Vector3.zero;
     private CharacterController controller;
 
 
     // Use this for initialization
     void Start () {
         controller = GetComponent<CharacterController>();
         animator = GetComponent<Animator>();
     
     }
     
     // Update is called once per frame
     void Update () {
 
         //This code is for running
         float inputx = Input.GetAxis ("Horizontal");
         float inputy = Input.GetAxis ("Vertical");
 
         movement = new Vector2 (speed.x * inputx, inputy);
 
         //Plays animation if the player is running
         if ((Input.GetKeyDown(KeyCode.RightArrow)) || (Input.GetKeyDown(KeyCode.LeftArrow))){
         
             animator.SetBool("isRunning", false);
         }
         if ((Input.GetKeyUp(KeyCode.RightArrow)) || (Input.GetKeyUp (KeyCode.LeftArrow))){
             animator.SetBool ("isRunning", true);
         }
 
         //this makes the player jump
         if(Input.GetKeyDown(KeyCode.UpArrow)){
             var y = Input.GetAxis("Vertical") + 1;
             transform.Translate(0, y, 0);
         }
 
             
 
     
     
     }
 
     void FixedUpdate(){
         //this is also for running
         rigidbody2D.velocity = movement;
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Slvr99 · May 30, 2014 at 01:26 AM

For smooth movement, use Vector3.Lerp(). Instead of transform.Translate, use this:

 transform.position = Vector3.Lerp(transform.position, Vector3(0, y, 0), Time.deltaTime * speed);

speed is a float of how fast you want it to move. You can either make that a new variable for easy editing or replace it with a float.

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 maggieconnell · May 30, 2014 at 01:49 AM 0
Share

Thank you so much! I think this is getting me closer to a solution, but it doesn't quite work. I am getting this error: "Expression denotes a type', where a variable', value' or method group' was expected". I'm trying to read up on Vector3.Lerp() to see if I can figure this out myself... I think it might have something to do with the second argument, Vector3(0, y, 0).

avatar image Slvr99 · May 30, 2014 at 02:02 AM 0
Share

$$anonymous$$ake sure that the word "speed" in this is either a float(ex. 5.0f), or a defined variable in the script(ex. at the top, have public float speed = 5.0f;) Also, I don't use C#, but you may need to add "new" right before the "Vector3(0, y, 0)" in it.

avatar image MikeNewall · May 30, 2014 at 02:04 AM 0
Share

Add the "new" keyword in front of "vector3(0,y,0)"

You may also want to change that piece of code as lerping the x and z values to 0 will transform your player back to the center of the world... use the current x and z position ins$$anonymous$$d

 new vector3(transform.position.x, y, transform.position.z)
avatar image maggieconnell · May 30, 2014 at 02:12 AM 0
Share

Aha! That's it! The second argument also had to be changed slightly to: new Vector3(0, y, 0)

Thank you for your help!

The jumping is still pretty rough... but I think that might just be an issue of figuring out the right speed and distance...

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

22 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Wall Jumping Help 1 Answer

How to insert a touch guitexture into playercontrols 1 Answer

The Mario Jump? 2 Answers

3D Platform Controller Forward & Camera HELP! 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