- Home /
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;
}
}
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.
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).
$$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.
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)
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
Follow this Question
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