Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NutellaDaddy · May 21, 2014 at 12:31 AM · c#errorrigidbody

Can't convert int to bool?

I'm not trying to convert an int to a bool. I am scripting in c sharp and when I come to this line it throws out this error!

r.drag = body.grounded ? 1 : 0.1f;

Why does this error pop up? r is the name that I gave to the rigidbody of body, grounded is a bool that shoes whether or not the player is touching the ground. Thanks for any help!

Comment
Add comment · Show 11
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 Jeff-Kesselman · May 21, 2014 at 12:33 AM 1
Share

body.grounded is probably an int.

You need to start a trinary operator with a boolean expression.

What are you actually trying to accomplish with that line of code?

If thats not it, then it is likely that r.drag is a boolean, but i suspect the former.

avatar image Kiwasi · May 21, 2014 at 12:44 AM 0
Share

RigidBody.drag is a float. Don't know if this helps

avatar image NutellaDaddy · May 21, 2014 at 12:44 AM 0
Share

Here's the full script if you could give me a write out solution.

 using UnityEngine;
 using System.Collections;
 
 public class GravityAttractor : $$anonymous$$onoBehaviour 
 {
     //SET TO TRUE FOR $$anonymous$$ONO-DIRECTIONAL GRAVITY
     private bool useLocalUpVector = false;
     //FORCE APPLIED ALONG GRAVITY UP VECTOR(NEGATIVE = DOWN)
     public float gravityForce = -10;
 
     public void Attract(PlanetaryGravity body)
     {
         Vector3 gravityUp;
         Vector3 localUp;
         Vector3 localForward;
 
         Transform t = body.transform;
         Rigidbody r = body.rigidbody;
 
         //FIGURE OUT THE BODIES UP VECTOR
         if(useLocalUpVector)
         {
             gravityUp = transform.up;   
         } 
         else 
         {
             gravityUp = t.position - transform.position;
             gravityUp.Normalize();
         }
 
         // Accelerate the body along its up vector
         r.AddForce( gravityUp * gravityForce * r.mass );
         r.drag = body.grounded ? 1 : 0.1f;
 
         // If the object's freezerotation is set, we force the object upright
         
         if(r.freezeRotation)
         {
             
             // Orient relatived to gravity
             localUp = t.up;
             var q = Quaternion.FromToRotation(localUp, gravityUp);
             q = q * t.rotation;
             t.rotation = Quaternion.Slerp(t.rotation, q, 0.1f);
             localForward = t.forward;    
         }
 
     }
 }
 
avatar image rutter · May 21, 2014 at 12:54 AM 0
Share

What's the exact text of the compile error?

You may be able to fix this by changing 1 to 1f. The two outputs of a ternary operator must have a common type; you're offering an int and a float, and I don't know offhand if the compiler will cast implicitly.

avatar image NutellaDaddy · May 21, 2014 at 12:57 AM 0
Share

t might be a problem with my conversion from Javascript to C Sharp?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · May 21, 2014 at 02:28 AM

Since your "grounded" variable is an int you have to convert it to a boolean state "is grounded". From your code it seems that you're grounded when the "grounded" integer is greater than 0, so just use the greater-than operator like this:

     r.drag = (body.grounded > 0) ? 1f : 0.1f;

Alternatively you could also add this property to your PlanetaryGravity class:

     public bool IsGrounded
     {
         get {return grounded > 0;}
     }

and use the property instead:

     r.drag = body.IsGrounded ? 1f : 0.1f;
Comment
Add comment · 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
0

Answer by zsolti · Jan 06, 2015 at 02:26 PM

bool abc=(x == 1); if (x==1) //abc=true else //abc=false

Comment
Add comment · 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

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

25 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

how to create Magnetic repulsion ? 0 Answers

Weird collision error when using waypoint script... 1 Answer

Animation spins wildly after completed 0 Answers

Getting index of the smallest number in a list 1 Answer


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