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;
}
}