How can I make the charecter jump in my game with this script I have?
Bellow is my code...This script sort of works for jumping in the sense that it will move the player upwards however not only is the falling very slow and zero gravity like but also if you spam the space bar you just keep going upwards so it essentially helps you to fly instead. Does anyone have any ideas to fix this script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour {
public float speed = 10.0F;
public float sprintSpeed = 20.0F;
public float jumpheight = 5.0F;
private Rigidbody rb;
// Use this for initialization
void Start () {
Cursor.lockState = CursorLockMode.Locked;
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
float translation = Input.GetAxis ("Vertical") * speed;
float straffe = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate (straffe, 0, translation);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
if (Input.GetKeyDown ("r"))
speed = sprintSpeed;
if (Input.GetKeyUp ("r"))
speed = 10.0F;
}
void FixedUpdate() {
if (Input.GetKeyDown("space"))
rb.velocity = new Vector3(0, 10 * jumpheight * Time.deltaTime, 0);
}
}
Also, I have trying the jump script in just the update function but that didn't work either. Didn't think it would though.
Your answer
Follow this Question
Related Questions
Most suitable way to make player move for an open world environment? 0 Answers
Issues with making a climbing script compatible with CharacterController 0 Answers
Why pressing the up and down key doesn't mean the body in correct forward and back direction? 1 Answer
Character moves opposite ways.,My character moves perfectly to every direction except -z. 0 Answers