Basic MOVE and JUMP script (Jump trouble)
Hello,
I need help with fixing script, I simply need basic ASDW movement and one hit SPACE jump (forbidden jump while in air) with public floats so I can test it and set the right values. (need to jump around 110cm fast).
I use just Capsule with rigid body, capsule collider.
What I have now work for movement but jump is more like jetpack, it does slowly speed upwards.
I would be glad if somebody would be so kind and helped me with jump issue as I am not programmer, just need this for level design setting/browsing.
Would prefer posting whole functioning code for the same reason.
Thanks in advance.
using UnityEngine;
using System.Collections;
public class CharacterController : MonoBehaviour {
private Rigidbody rg;
public float speed = 10.0F;
public float jumpspeed = 10.0F;
void Start () {
Cursor.lockState = CursorLockMode.Locked;
rg = GetComponent <Rigidbody> ();
}
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.GetKey (KeyCode.Space)) {
Vector3 atas = new Vector3 (0,100,0);
rg.AddForce(atas * speed);
}
if (Input.GetKeyDown ("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
Answer by KoenigX3 · Jan 05, 2017 at 12:12 AM
First of all, you should use Input.GetKeyDown instead of Input.GetKey at the jumping method, since you want to receive only one input. This will get rid of the jetpack-feeling. Also, you could increase the speed value, so you get the right amount of upward movement.
You can use the Physics.Raycast method to check if there is a solid ground under the capsule. Note that it only works, if the ground object has a collider component attached to it.
// This should be the half of the height of the capsule plus a bit more
// Or else it will fail to meet the collider of the ground object
// This default value is ideal for a capsule with a height of 1.0F
public float raycastLength = 0.52F;
void Update()
{
if(Physics.Raycast(transform.position, -transform.up, raycastLength))
{
if(Input.GetKeyDown(KeyCode.Space))
{
Vector3 atas = new Vector3 (0,100,0);
rg.AddForce(atas * speed);
}
}
}
Answer by GurkMuppen · Jan 05, 2020 at 12:58 PM
I merged two already existing scripts for movement and jumping. Before you use it you will need a tag on the ground (the object that your player stands on) as "Ground". Here it is:
public class PlayerController : MonoBehaviour {
Rigidbody rb; public bool isGrounded; public float jumpHeight = 5f; public float Speed = 2f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == ("Ground") && isGrounded == false)
{
isGrounded = true;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
isGrounded = false;
}
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
rb.AddForce(new Vector3(moveHorizontal, 0.0f, moveVertical) * Speed);
}
}