Hey guys, just want to say thanks for any insight I can get on this situation.
Whenever I sprint (Holding Left Shift) and pres space (To Jump), I don’t jump. I can only jump while I am not sprinting.
Does anyone have experience on this? It appears theirs a conflict from having the same button held down for a different input?
private void Update ()
{
speed = 5;
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 10;
}
Jump ();
}
//
private void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (IsGrounded())
{
rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
}
}
}
What conflict? They are two different buttons. I would guess it is to do with your IsGrounded method not liking it when you move faster.
2 Likes
In what way would this affect the sprinting code? I’ve tried extending my raycast beyond lower limits and even then, I can’t spring and jump. Please anything helps.
First thing is to add Debug.Logs to see where it’s stopping and why.
second thing is to post the whole script, or at least every function related.
1 Like
Apologize the ignorance, but where what is stopping?
And here is the full control code.
using UnityEngine;
public class PlayerMovementController : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private float jumpForce;
[SerializeField] private float jumpRaycastDistance;
private Rigidbody rb;
private void Start ()
{
rb = GetComponent<Rigidbody>();
}
private void Update ()
{
speed = 5;
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 10;
}
Jump ();
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
float hAxis = Input.GetAxisRaw("Horizontal");
float vAxis = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.fixedDeltaTime;
Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement);
rb.MovePosition(newPosition);
}
private void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (IsGrounded())
{
rb.velocity += new Vector3(0, jumpForce, 0);
}
}
}
private bool IsGrounded ()
{
return Physics.Raycast(transform.position, Vector3.down, jumpRaycastDistance);
}
}
SparrowsNest:
First thing is to add Debug.Logs to see where it’s stopping and why.
second thing is to post the whole script, or at least every function related.
The execution leading to the “rb.velocity += new Vector3(0, jumpForce, 0);”
line, see if IsGrounded is returning false for example.
you can also use Debug.DrawRay to see where the ray is at in real time and use colors to see if it’s hitting something.
private bool IsGrounded ()
{
if(Physics.Raycast(transform.position, Vector3.down, jumpRaycastDistance)){
Debug.DrawRay(transform.position, Vector3.down * jumpRaycastDistance, Color.red);
return true;
} else {
Debug.DrawRay(transform.position, Vector3.down * jumpRaycastDistance, Color.green);
return false;
}
}
1 Like
I tried the code, but the Raycast was not appearing.
So I then tried removing the grounded function all together.
But as an update, after further troubleshooting I was able to find that this error only occurs when I’m sprinting forward.
When I sprint backwards, or sideways, I am able to jump mid sprint.
SparrowsNest:
The execution leading to the “rb.velocity += new Vector3(0, jumpForce, 0);”
line, see if IsGrounded is returning false for example.
you can also use Debug.DrawRay to see where the ray is at in real time and use colors to see if it’s hitting something.
private bool IsGrounded ()
{
if(Physics.Raycast(transform.position, Vector3.down, jumpRaycastDistance)){
Debug.DrawRay(transform.position, Vector3.down * jumpRaycastDistance, Color.red);
return true;
} else {
Debug.DrawRay(transform.position, Vector3.down * jumpRaycastDistance, Color.green);
return false;
}
}
Tick on Gizmos in the game view window, top right.
1 Like
After turning on Gizmo I saw that while i’m near the ground the line appears Red, once I’m in the air it is Green.
I think the issue wouldn’t be related in this case.
It’s only happening while I sprint forward. Not any other direction.
While you are running, does it stay solid red or does it flicker between red and green?
how about those Debug.Logs we talked about earlier?
1 Like
While I am running I am not seeing any flicker of the line, although I forgot to read the console to check for the debug.
I am currently at work, but will be home in 9 hours. Thanks for staying in touch. This is an incredible learning experience.
SparrowsNest:
While you are running, does it stay solid red or does it flicker between red and green?
how about those Debug.Logs we talked about earlier?
It’s coming up red, what should be done in this case?
Well red means the IsGrounded returns true and probably isn’t the problem, flood your code with statements such as
private void Update ()
{
speed = 5;
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 10;
Debug.Log("Sprinting");
}
if (Input.GetKeyDown(KeyCode.Space))
{
Jump ();
Debug.Log("Jump :: Input");
}
}
to see the path your code takes, where he’s stopping, you can also print out variable value if needed.
also, as a side note, why do I always see newbies write stuff like this… is this just an innocent mistake or is someone teaching you bad practices? (not that i’m dissing you, we’ve all had to learn this stuff some how)
notice that I put the input check before the call to the Jump() function, this makes it so that the update loop only calls the Jump() function once when the input is true, in your code you call Jump() every iteration of the update loop and check there if the player wanted to jump or not, this makes for an extra function call and takes away CPU time, in this case it’s very minimal and you won’t experience lag because of this, but it adds up in bigger algorithms and is generally bad practice.
1 Like
Thanks for the advice on organizing the code this way. It also looks cleaner in general.
Now that this debug is active, I found that while sprinting, the input for jumping does not enter at all?
SparrowsNest:
Well red means the IsGrounded returns true and probably isn’t the problem, flood your code with statements such as
private void Update ()
{
speed = 5;
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 10;
Debug.Log("Sprinting");
}
if (Input.GetKeyDown(KeyCode.Space))
{
Jump ();
Debug.Log("Jump :: Input");
}
}
to see the path your code takes, where he’s stopping, you can also print out variable value if needed.
also, as a side note, why do I always see newbies write stuff like this… is this just an innocent mistake or is someone teaching you bad practices? (not that i’m dissing you, we’ve all had to learn this stuff some how)
notice that I put the input check before the call to the Jump() function, this makes it so that the update loop only calls the Jump() function once when the input is true, in your code you call Jump() every iteration of the update loop and check there if the player wanted to jump or not, this makes for an extra function call and takes away CPU time, in this case it’s very minimal and you won’t experience lag because of this, but it adds up in bigger algorithms and is generally bad practice.
Are you sure there isn’t a “return” in the sprint or a “else” on the second if?
is this the code 1 to 1?
this is very weird, I don’t see any reason for this not to work.
try maybe opening a new project and putting only that script in, if it works there and not in the original project there’s something conflicting with it, any thing else directly related to this script/rigidbody/player/kajigger/thingy?
1 Like
Only other script is for the fps camera. Nothing connected tho. :C
And sorry, but I don’t know what 1-1 is and I couldn’t find anything about this on google!
This is such a strange issue.
SparrowsNest:
Are you sure there isn’t a “return” in the sprint or a “else” on the second if?
is this the code 1 to 1?
this is very weird, I don’t see any reason for this not to work.
try maybe opening a new project and putting only that script in, if it works there and not in the original project there’s something conflicting with it, any thing else directly related to this script/rigidbody/player/kajigger/thingy?
Not a technical term, i asked if this is exactly (one to one) the code you have running in the project, it behaves as if there’s and extra “return” or “else” there.
if no one else have an idea whats going on i say upload the project so we can take a look for ourselves, i’d gladly look over it during the weekend.
1 Like
You’re amazing, thank you so much. How would I send you the project? Direct message or uploading it on here?
And yes this is the exact code.
SparrowsNest:
Not a technical term, i asked if this is exactly (one to one) the code you have running in the project, it behaves as if there’s and extra “return” or “else” there.
if no one else have an idea whats going on i say upload the project so we can take a look for ourselves, i’d gladly look over it during the weekend.
Also, as a troubleshooting method, what could be done is the Ray was green and not reading on the floor? Because that continuously appears to be happening. Is there anyway the Sprint function is turning off the (Grounded) ray?
MikawasakiDigital:
You’re amazing, thank you so much. How would I send you the project? Direct message or uploading it on here?
And yes this is the exact code.
I just try and provide as much help as I got when I fist started
You can create a unity package and upload that, but stuffing the project file into an upload site is good too.
MikawasakiDigital:
Also, as a troubleshooting method, what could be done is the Ray was green and not reading on the floor? Because that continuously appears to be happening. Is there anyway the Sprint function is turning off the (Grounded) ray?
I didn’t understand what you said about the ray here at all, it might just be me and my crazy lack of sleep driving me crazy, haha.
1 Like