- Home /
Question by
Raggo3D · Jun 02, 2016 at 09:30 AM ·
c#jumpjumpingplatformerjumping object
Can't get platforming character to jump
Been scratching my head for days. Here's my script
public class characterMovement : MonoBehaviour {
public float speed = 6.0f;
public float gravity = -75.0f;
public float jumpSpeed = 100.0f;
private Vector3 moveDirection = Vector3.zero;
void Start ()
{
}
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
//checking if grounded
if (controller.isGrounded)
{
print("grounded!");
if (Input.GetKeyDown(KeyCode.Joystick1Button0))
{
moveDirection.y = jumpSpeed;
print("we jumpin");
}
}
//move the thing
moveDirection.y = gravity;
controller.Move(moveDirection * Time.deltaTime);
}
}
When I play the game and hit the "A" button on the 360 controller, I get my "we jumpin" message, which tells me its receiving the input, but I still can't get the character to jump. My public floats are the same in the script as they are in the unity editor.
Comment
Answer by allenallenallen · Jun 02, 2016 at 09:33 AM
When adding jump height or applying gravity, always add or subtract, not make it directly equal.
moveDirection.y += jumpSpeed;
moveDirection.y += gravity; // Since your gravity is negative, I used +
Answer by $$anonymous$$ · Jun 04, 2016 at 03:53 PM
Maybe use this:
if(someKeyTouched)
myRigidBody.AddForce(transform.up * jumpForce);
this script will make jump your rigidbody (2d or 3d) + if you add physics materia, you can make it bounce :)