How do I make my player jump?
So I'm still in the beginning of learning C# and using Unity but I'm trying to make my player jump. I just got it to move and everything with a rigid body. It sort of works, when I press the jump button it jumps but there are several things wrong with the jump itself.
The jump is not very high. My jump multiplier is set to 100 currently and the jump is still pretty wimpy. Is this sort of "inflation" in my units normal?
The jump overrides all existing forces. When I press the jump it stops all existing movement to jump. So if I'm moving forward and press jump it stops moving and then jumps instead of jumping forward. I know this is because my script overrides and sets new forces but I don't know what to change.
The fall is slow. Incredibly slow. It's like the 99 Red Balloons just slowly floating to the ground and I really need my player to not start a nuclear war. More importantly, I want the player to fall realistically and usefully. Right now it just slowly moves down.
The jump repeats. If I press the jump again while in the air it will always jump again. I think I need a bool to check if it's on the ground or not but I don't know how to go about this.
Here is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement : MonoBehaviour {
public float movementSpeed; //movement multiplier
public float rotationSpeed; //rotation multiplier
public float jumpAmount; //jumping multiplier
public Rigidbody rb; //rigid body
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if(Input.GetKey (KeyCode.W) == true){
rb.velocity = transform.forward * movementSpeed;
}
if(Input.GetKey (KeyCode.S) == true){
rb.velocity = transform.forward * -movementSpeed;
}
if(Input.GetKey (KeyCode.A) == true){
rb.angularVelocity = new Vector3 (0, -1, 0) * rotationSpeed;
}
if(Input.GetKey (KeyCode.D) == true){
rb.angularVelocity = new Vector3 (0, 1, 0) * rotationSpeed;
}
if(Input.GetKeyDown (KeyCode.Space) == true){
rb.AddForce (Vector3.up * jumpAmount, ForceMode.Impulse);
}
}
}
Currently on my Rigidbody, the Mass is set to 10, Drag to 10, and Angular Drag to 10. It is using gravity. Nothing else is different from defaults except the X and Z rotation are frozen to keep the capsule from falling over. My Movement Speed is 15 my Rotation Speed is 6 and my Jump Amount is 100
Any help and tips would be great. I'm looking for help with my jump but if I'm doing something else wrong feel free to say so.
Answer by handsomeTown · Apr 27, 2017 at 07:45 AM
I'm a baby at Unity, so excuse me if this is a stupid question, but have you checked the size of the player? I've noticed everything I drag into my scene has to be shrunk like 90% to even be manageable, so I'm wondering if it could be that it is jumping 100 units, but the player is 10,000 units tall or something.
Answer by Jawchewa · Apr 28, 2017 at 02:06 AM
I think the main issue with your jumps currently is your drag factor. If you set your drag to 0, your jumps should be fixed. You will also probably need to adjust your movement speed to account for the new drag, and you might need to play with a few other variables to make it work the way that you want.
Answer by AUSLER · Jun 09, 2017 at 04:51 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float movementSpeed = 30.0f;
private float Vector3;
public float jumpSpeed = 10f;
public float gravity = 9.8f;
void Start () {
Cursor.visible = false;
}
// Update is called once per frame
void Update ()
{
//walking
var x = Input.GetAxis ("Horizontal") * Time.deltaTime * movementSpeed;
var z = Input.GetAxis ("Vertical") * Time.deltaTime * movementSpeed;
transform.Translate (0, 0, z);
transform.Translate (x, 0, 0);
//jump
if (Input.GetKeyDown (KeyCode.Space))
transform.position.y = transform.position.y + 2;
}
}
this is code i use for jumping and stuff it should problem 1,2 and 4. but the fall is still very slow with this one, if you can fix that as well everything should work (i mostly work with spheres and basic shapes) @The_Pied_Shadow ,using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float movementSpeed = 30.0f;
private float Vector3;
public float jumpSpeed = 10f;
public float gravity = 9.8f;
void Start () {
Cursor.visible = false;
}
// Update is called once per frame
void Update ()
{
//walking
var x = Input.GetAxis ("Horizontal") * Time.deltaTime * movementSpeed;
var z = Input.GetAxis ("Vertical") * Time.deltaTime * movementSpeed;
transform.Translate (0, 0, z);
transform.Translate (x, 0, 0);
//jump
if (Input.GetKeyDown (KeyCode.Space))
transform.position.y = transform.position.y + 2;
}
}
this is code i use for jumping and stuff it should problem 1,2 and 4. but the fall is still very slow with this one, if you can fix that as well everything should work (i mostly work with spheres and basic shapes)
sorry replace if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) transform.position.y = transform.position.y + 2;
with
var y = Input.GetAxis ("Vertical") Time.deltaTime movementSpeed;
transform.Translate (0, y, 0);
Your answer
Follow this Question
Related Questions
Object Player Movement stops working after Restarting Unity 0 Answers
How to move 3 players at the same time in V form,How to move 3 characters in the same time in V form 0 Answers
Reduce Player Movement in Air 0 Answers
Unexisting blocks colliding and existing block not colliding 1 Answer
Player only moves when the key is pressed, not when it's held. 0 Answers