- Home /
Question by
QC_Scott · Jan 27, 2014 at 09:49 PM ·
gravity2d sprites2d rotation
Gravity not working to right strength
I recently created a movement script that will only allow movement in the air; there are two problems: 1. Gravity isn't applying strongly enough, the object falls very slowly. 2. If you keep hitting the space bar you can continue to jump higher and higher.
Here is the code:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
// Use this for initialization
void Start () {
}
public float speed = 0.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float jumpMove = 6.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController> ();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
//Multiply it by speed.
moveDirection *= speed;
}
else {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
//Multiply it by speed.
moveDirection *= jumpMove;
}
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
Can someone please help me fix these problems?
Comment
Your answer
