Anyone have any idea why my jump function isn’t working for my vehicle? I hammer the space button and it just won’t do anything :')
And yes I have attached the rigidbody of the car to the slot
public class vehicleController : MonoBehaviour {
public float carSpeed;
public float maxPos = 3.6f;
public bool hasAddedSpeed = false;
public uiManager ui;
public GameObject car;
Vector3 position;
public Rigidbody rb;
public int jumpSpeed;
void Start ()
{
//ui = GetComponent<uiManager> ();
position = transform.position;
rb = GetComponent <Rigidbody>();
}
void Update ()
{
if(!hasAddedSpeed)
{
if(ui.score >= 10)
{
carSpeed = carSpeed + 2;
hasAddedSpeed = true;
}
}
position.x += Input.GetAxis ("Horizontal") * carSpeed * Time.deltaTime;
position.x = Mathf.Clamp (position.x,-maxPos,maxPos);
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(0,10,0 * jumpSpeed);
}
transform.position = position;
0 times anything is 0, not sure what you’re trying to do there but thats not going to help.
the default forcemode is “Force”, which is force applied over time, but GetKeyDown only fires in the one frame where the key is pressed. So you’re only ever going to get a single frame’s worth of force applied, which is peanuts.
Try ForceMode.Impulse, all the force applied in one go
You’re explicitly setting the position every frame. Not really sure how else to help you. You need to either a) stop doing that or b) set your position’s y variable equal to the current height of your jump.
You are using the rigidbody to add a force for your jump. Then you just ignore what the rigid body is doing by reseting the position of the game object directly.
A couple of things here:
You shouldn’t be using AddForce in update. It should be in FixedUpdate as its related to physics. You do need to keep your button check in Update though - use a bool to tell FixedUpdate to apply jump code.
You are moving your car by directly setting the transform but also expect physics to just work. You probably shouldn’t be doing both. RigidBody.MovePosition is a better method here as it will respect the collision settings of the rigidbody.
This is not tested, but how about something like this:
So I got a little bit more interested in this and wrote a version with a ground check to stop endless jumping. You will need to add the ui.score stuff back in as I deleted to allow this to compile in my test.
using UnityEngine;
using System.Collections;
public class driver : MonoBehaviour {
// car settings
public int JumpSpeed;
public int CarSpeed=2;
public float MaxPos=3.6f;
public GameObject Car;
// things for grounded check
// the layer(S) that have your ground in it - the care mustnt be in this layer
public LayerMask GroundLayers;
public float DistanceToCheck=0.5f;
protected bool mIsGrounded=true;
// internal stuff
private Rigidbody mRB;
private bool mAddJumpForce=false;
// Use this for initialization
void Start () {
mRB = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
mAddJumpForce = Input.GetKeyDown (KeyCode.Space) && mIsGrounded;
}
void FixedUpdate(){
// jump or move
if (mAddJumpForce) {
mRB.AddForce (Vector3.up * JumpSpeed, ForceMode.Impulse);
mAddJumpForce = false;
} else {
Vector3 _position = mRB.position;
_position.x += Input.GetAxis ("Horizontal") * CarSpeed * Time.deltaTime;
_position.x = Mathf.Clamp (_position.x,-MaxPos,MaxPos);
mRB.MovePosition (_position);
}
mIsGrounded = Physics.Raycast (mRB.position, -gameObject.transform.up, DistanceToCheck, GroundLayers);
}
}
Done everything you said and created a new layer called Ground and attached it to my ground object. The car is not in this layer and is on the “default layer” Even still however the car is just refusing to do anything when i try and make it jump. Might this have something to do with the box collider on the vehicle?
Here’s the code anyway in case i overlooked anything!
Seems like you have mixed the code with your version, and left a lot of the old problems in it. Use this, exactly as it is, I have merged your stuff into it:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class driver : MonoBehaviour {
#region Variables
// character settings
public int JumpSpeed=2;
public int CharSpeed=2;
public float MaxHorizontalMove=3.6f;
// things for grounded check
// the layer(S) that have your ground in it - the care mustnt be in this layer
public LayerMask GroundLayers;
public float DistanceToCheck=0.5f;
protected bool mIsGrounded=true;
// internal stuff
private Rigidbody mRB;
private bool mAddJumpForce=false;
private bool hasAddedSpeed=false;:
public uiManager ui;
#endregion
#region Unity Events
// Use this for initialization
void Start () {
mRB = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if(!hasAddedSpeed)
{
if(ui.score >= 10)
{
CarSpeed = CarSpeed + 2;
hasAddedSpeed = true;
}
}
mAddJumpForce = Input.GetKeyDown (KeyCode.Space) && mIsGrounded;
}
void FixedUpdate(){
// jump or move
if (mAddJumpForce) {
mRB.AddForce (Vector3.up * JumpSpeed, ForceMode.Impulse);
mAddJumpForce = false;
} else {
Vector3 _position = mRB.position;
_position.x += Input.GetAxis ("Horizontal") * CharSpeed * Time.deltaTime;
_position.x = Mathf.Clamp (_position.x,-MaxHorizontalMove,MaxHorizontalMove);
mRB.MovePosition (_position);
}
mIsGrounded = Physics.Raycast (mRB.position, -gameObject.transform.up, DistanceToCheck, GroundLayers);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Enemy Car") {
ui.Dead ();
}
}
#endregion
}
Works pretty much perfectly! Thank you so much :')
However there is one tiny issue where the vehicle hits the ground. On some occasions I can jump as soon as i high the ground (which is what I want) and something it takes a few seconds for me to be able to jump again which comes off as a little unresponsive in a fast paced game. Anyway I Can adjust the code to remedy this :)??
Yeah that happens because GetKeyDown will only return true in the one frame that it is pressed while GetKey just keeps returning true while that key is down.
The problem happens when you start holding the key when you are not grounded expecting the jump to happen when you become grounded. But it will fail and GetKeyDown will be returning false.
Nastly little bug!
If you are interested, I turned this code into an endless runner component - with a few extra features - air movement on/of and the ability to move forward and backwards in the z direction as well. The MaxPositionOffset vecotors Y component has that value.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class Driver : MonoBehaviour {
#region Variables
// character settings
public int JumpSpeed=10;
public int CharSpeed=2;
public Vector2 MaxPositionOffset = new Vector2(3.6f, 3.6f);
public bool AllowAirControl=true;
// things for grounded check
// the layer(S) that have your ground in it - the care mustnt be in this layer
public LayerMask GroundLayers;
public float DistanceToCheck=0.5f;
protected bool mIsGrounded=true;
// internal stuff
private Rigidbody mRB;
private bool mAddJumpForce=false;
#endregion
#region Unity Events
// Use this for initialization
void Start () {
mRB = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
// is the button down and the character grounded? no air jumping here!
mAddJumpForce = Input.GetKey (KeyCode.Space) && mIsGrounded && !mAddJumpForce;
}
void FixedUpdate(){
// jump or move this frame - although air moving is accepted
// we will onky do one "thing" this frame
if (mAddJumpForce) {
mRB.AddForce (Vector3.up * JumpSpeed, ForceMode.Impulse);
mAddJumpForce = false;
} else if(mIsGrounded || (!mIsGrounded && AllowAirControl)) {
// NOTE: this assumes the player is centered at the origin
// to change this you will have to modify the how the position is
// clamped (we are using exact values - not offsets)
Vector3 _position = mRB.position;
float _horizontal, _vertical;
_horizontal = Input.GetAxis("Horizontal");
_vertical = Input.GetAxis("Vertical");
if(!Mathf.Approximately(_horizontal,0.0f))
{
_position.x = Mathf.Clamp(_position.x + (Input.GetAxis("Horizontal") * CharSpeed * Time.deltaTime),
-MaxPositionOffset.x, MaxPositionOffset.x);
}
if(!Mathf.Approximately(_vertical, 0.0f))
{
_position.z = Mathf.Clamp(_position.z + (Input.GetAxis("Vertical") * CharSpeed * Time.deltaTime),
-MaxPositionOffset.y, MaxPositionOffset.y);
}
mRB.MovePosition (_position);
}
// it might be possible to limit this check, if you know that jumping is the only way that
// the player will leave the ground you could set mIsGrounded to false in the jump method
// and add an if around this to only execute when mIsGrounded is false. However, as a generic component
// we cannot make that assumption as other game effects may make the player jump.
mIsGrounded = Physics.Raycast (mRB.position, -gameObject.transform.up, DistanceToCheck, GroundLayers);
}
void OnCollisionEnter(Collision col)
{
}
#endregion
}
Thanks a lot for all the effort but right now I’m really happy with what I’ve got! Might be very useful for fellow users tho :')!
I wish there was some way I could show you my progress besides screenshots since the webplayer Doesn’t work anymore :(!