Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Datapax · Jan 04, 2017 at 07:01 PM · booleanbool

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;
     }
 }

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Habitablaba · Jan 04, 2017 at 11:13 PM 1
Share

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.

avatar image Datapax Habitablaba · Jan 04, 2017 at 11:58 PM 0
Share

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 : )

avatar image Habitablaba Datapax · Jan 05, 2017 at 12:07 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Datapax · Jan 04, 2017 at 08:13 PM 0
Share

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 : )

avatar image Creeper_Math Datapax · Jan 04, 2017 at 08:19 PM 0
Share

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"

avatar image Datapax Creeper_Math · Jan 04, 2017 at 09:01 PM 0
Share

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.

Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges