- Home /
Unable to set boolean to true
The problem is that running the code below, the "jump" bool never becomes "true".
I am using the Debug.Log
to check that the ground distance is bigger than "0" and it is when I make the click and the object moves, but the bool is still "false", and just in a few milliseconds the objects just goes back down. It never completes the Lerp.
If I take out the statement:
if (gd == 0.0f)
{
jump = false;
}
... it works. But ofcourse, I can never set "jump" to false that way.
Well, I suppose it is something I'm overlooking but, I just can't see what. Or maybe just my logic is crap :(
Please, can anyone tell me what is wrong here.
using UnityEngine;
using System.Collections;
public class MoveControl : MonoBehaviour {
private bool jump;
private float distance = 4.5f;
private float time = 0.15f;
private float gd;
private float yVelocity = 0.0f;
private float gravity = 9.81f;
private Vector3 target;
private Vector3 point;
void Start()
{
point = transform.position;
jump = false;
}
void Update()
{
gd = GroundDistance();
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
point = ray.origin + (ray.direction * distance);
point.z = 0.0f;
jump = true;
}
if(jump)
{
transform.position = new Vector3((Mathf.Lerp(transform.position.x, point.x, time)), (Mathf.Lerp(transform.position.y, point.y, time)), transform.position.z);
}
if(gd > 0.0f)
{
yVelocity -= gravity * Time.deltaTime;
transform.position = new Vector3(transform.position.x, (transform.position.y + (yVelocity * Time.deltaTime)), transform.position.z);
}
if (gd == 0.0f)
{
jump = false;
}
Debug.Log("Jump: " + jump);
Debug.Log(gd);
}
public float GroundDistance()
{
RaycastHit hit;
Physics.Raycast(transform.position, Vector3.down, out hit, 10.0f);
// Debugging line representing the Raycast ray, only in editor mode for development
Debug.DrawLine(transform.position, hit.point, Color.red, 1.0f, false);
return hit.distance;
}
}
Take a look at what is happening when you try to jump:
if(jump)
{
transform.position = new Vector3(($$anonymous$$athf.Lerp(transform.position.x, point.x, time)), ($$anonymous$$athf.Lerp(transform.position.y, point.y, time)), transform.position.z);
}
if(gd > 0.0f)
{
yVelocity -= gravity * Time.deltaTime;
transform.position = new Vector3(transform.position.x, (transform.position.y + (yVelocity * Time.deltaTime)), transform.position.z);
}
if (gd == 0.0f)
{
jump = false;
}
Assume jump is "true" because we've just clicked mouse button 0.
gd will be 0 at this point because we've not jumped yet.
We fall into the first if statement.
We set transform.position to a new value by setting the x to 0.15 of the way between the current x and the x of the location calculated previously, doing the same for y, and keeping the z. It should be noted that this is super weird and probably not what you're intending. This will be very close to not moving at all.
We fail the next if check because gd is 0.
We fall into the final if statement because gd is still 0, setting jump to false.
Now, every time Update is called (and we've not clicked again), jump is going to be false.
Hey thanks for your suggestion. I actually had some time on this and finally came up with the problem.
It was the fact that in the second if
statement I am pulling the game object back down by a way higher amount than even 0.1f
... so as soon as I jump in the first if
and gain some height on the Y
vector, the second if
statement is pulling the object back down to 0.0f
. And ofcourse, just after that the third if
statement is correctly returning true, because gd == 0.0f
Phew.... : D
Thanks again for your help Cheers
PD: And yes, it is a bit weird but, the project is a bit weird too and I'm trying my way out : )
I'm glad you've found and fixed some issues! Progress is great! But I do want to point out that since you aren't calculating gd between each if statement, it isn't 0 because you moved back by too much... it's 0 because that's what it was at the start of the frame. You then skip the part where you move it up every frame, because jump is set to false and never set back to true until you click again.
Answer by Creeper_Math · Jan 04, 2017 at 07:52 PM
You're probably never going to get a distance of exactly "0" when using it for detecting distance and such, Always let there be a little gap for the code to catch the statement. I suggest maybe setting it to 1, or if that's too much 0.5f.
Yes I thought about the same thing and I did try this but unfortunately it didn't make any difference.
Also, the problem is not that it is never hitting 0, actually it is always hitting 0 : D
I put a "Debug.Log" into the loop checking if ground is equal to 0 and it just logs out every frame, which should mean that, yes, it is actually detecting ground 0 every frame. However, when I make the click, the game object moves, and it changes its position, which I also Debug.Log on each frame.
It is very strange that the if (gd == 0.0f)
statement is triggering even if ground distance is bigger then "0"
Thanks for your suggestion anyway, I really appreciate it : )
For calculating distance, you're missing the point where the ray fails to hit anything.... Therefore it's returning 0... Ins$$anonymous$$d of the Physics.Raycast(transform.position, Vector3.down, out hit, 10.0f);
, try using
// Note I removed the 10.0f, I don't know what you're using there, but I would just use infinite distance for it anyways....
if (Physics.RayCast(transform.position, Vector3.down, out hit) {
return(hit.distance);
} else {
return(100000.0f); (or anything greatly beyond 0)
}
Another thing to ask is this question
"Is the RayCast hitting any colliders ON the player gameobject? If so that would explain why it's returning 0 every time"
This is working properly, I didn't explain it very well in my last reply.
The distance of the raycast is calculated properly. I check this with the Debug.Log on each update.
What I meant was that in the if
statement where it checks if gd == 0.0f
... it always hits 0. Well, I assume this because it returns true every frame. No matter that the ground distance is actually "not" equal to 0.
This statement just keeps returning true, and keeps setting my boolean to false : (
UPDATE: The raycast is always hitting, the ground. $$anonymous$$y object is always over the ground so it is always hitting it. And the 10.0f is just because I don't need the raycast to be longer than this, so no need to keep the ray extending further. However, I did try to leave it infinite but, still the same result.
Your answer
![](https://koobas.hobune.stream/wayback/20220612104054im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Set a Bool Through Function 1 Answer
Check if boolean is true on my gameObjects from my array 1 Answer
c# Ignoring conditional statement? 1 Answer
Unity UI Button Switch Bool? 2 Answers
What am I doing wrong with this bool? 3 Answers