- Home /
My player keeps launching into the sky
So the code is made and everything works except for jumping which i really need to get fixed so issue is when player is walking up hills or going up stairs and etc if you jump you launch into the sky and pretty much glide your back down.
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class PlayerMovement : MonoBehaviour{
[HideInInspector]
public bool sprinting;
[HideInInspector]
private Rigidbody selfRigidbody;
[HideInInspector]
public Option menukey;
[HideInInspector]
public KeyController ControllerCS;
[HideInInspector]
public bool isGrounded;
void Start()
{
selfRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
if (!menukey.MenuLock)
{
for (int i = 0; i < ControllerCS.KeyCS.Length; i++)
{
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Sprint")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
sprinting = true;
}
else
{
sprinting = false;
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Forward")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key) && sprinting)
{
transform.Translate(0, 0, 6f * Time.deltaTime);
}
else if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
transform.Translate(0, 0, 3f * Time.deltaTime);
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Back")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
transform.Translate(0, 0, -3f * Time.deltaTime);
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Right")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
transform.Translate(3f * Time.deltaTime, 0, 0);
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Left")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
transform.Translate(-3f * Time.deltaTime, 0, 0);
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Jump")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key) && isGrounded)
{
selfRigidbody.AddForce(0, 25, 0, ForceMode.Impulse);
isGrounded = false;
}
}
}
}
}
void OnCollisionStay()
{
isGrounded = true;
}
}
Hi. What is your expected behaviour, should it jump less or it should go forward while jumping ? $$anonymous$$y understanding so far your character just moves forward using transform translate, however, you can still climb because characters rigid-body pushed up by terrain or stairs. Could this be root of your unexpected behaviour ?
issue is that if i jump on incline the isGrounded bool I am guessing remains true and due to key being pressed it preforms a double jump or more, sending my characters higher into the air so i basically need help in figuring out how prevent that issue from reoccurring. I've tried yield and wait for seconds before and it did kinda work but there was still cases the issue would still occur.
Answer by darkal · Jul 12, 2018 at 08:51 AM
The issue i found out was i had jump key set as getkey rather than a getkeydown after changing it to proper one and adjusting the the force of the jump the script works fine but thankyou anyway.
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class PlayerMovement : MonoBehaviour{
[HideInInspector]
public bool sprinting;
[HideInInspector]
private Rigidbody selfRigidbody;
[HideInInspector]
public Option menukey;
[HideInInspector]
public KeyController ControllerCS;
[HideInInspector]
public bool isGrounded;
public float jumpforce;
void Start()
{
selfRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
if (!menukey.MenuLock)
{
for (int i = 0; i < ControllerCS.KeyCS.Length; i++)
{
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Sprint")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
sprinting = true;
}
else
{
sprinting = false;
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Forward")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key) && sprinting)
{
transform.Translate(0, 0, 6f * Time.deltaTime);
}
else if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
transform.Translate(0, 0, 3f * Time.deltaTime);
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Back")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
transform.Translate(0, 0, -3f * Time.deltaTime);
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Right")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
transform.Translate(3f * Time.deltaTime, 0, 0);
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Left")
{
if (Input.GetKey(ControllerCS.KeyCS[i].AllKeys.key))
{
transform.Translate(-3f * Time.deltaTime, 0, 0);
}
}
if (ControllerCS.KeyCS[i].AllKeys.keyname == "Jump")
{
if (Input.GetKeyDown(ControllerCS.KeyCS[i].AllKeys.key) && isGrounded)
{
selfRigidbody.AddForce(0, jumpforce, 0, ForceMode.Impulse);
isGrounded = false;
}
}
}
}
}
void OnCollisionStay()
{
isGrounded = true;
}
}
Your answer
Follow this Question
Related Questions
Gradual Speed Increase with Variable Input 0 Answers
Two near-identical scripts cause a cat to jump in different ways - why? 2 Answers
How do I make a jumping and ledge-grabbing system, similar to TLoZ OOT in unity 3d? 0 Answers
How to keep current velocity while jumping? 0 Answers
Movement with AddForce: Wrong Direction 2 Answers