- Home /
Movement script trouble
I've been making a 2d sidescroller game in Unity 3d, but I ran into a bit of a snag. When I use this script, the player goes all funky and walks really slow, and is also unable to jump normal, he only jumps about two milometers. So what is the problem?
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class Player : MonoBehaviour
{
private int _jumpSpeed = 10;
private int _gravity = 10;
private int _speed = 5;
private int _midAirSpeed = 2;
private Vector3 _moveDirection = Vector3.zero;
private Transform _transform;
private CharacterController _cc;
void Start ()
{
_cc = gameObject.GetComponent<CharacterController>();
_transform = transform;
}
void Update ()
{
_moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
if (_cc.isGrounded)
{
_moveDirection.x *= _speed;
if (Input.GetButton("Jump"))
_moveDirection.y = _jumpSpeed;
}
else
{
_moveDirection.x *= _midAirSpeed;
_moveDirection.y -= _gravity * Time.deltaTime;
}
_cc.Move(_moveDirection * Time.deltaTime);
}
}
Comment