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
1
Question by Gerard · Mar 26, 2010 at 03:57 PM · fps

(flags & CollisionFlags.CollidedBelow) != 0; ???

Hi,

I am trying to make some C# script, and it goes surprisingly good, but I got stuck by 2 lines I don't understand.

CollisionFlags flags = controller.Move(transform.forward * verticalInput + Vector3.up * upwardSpeed + addMovement);

grounded = (flags & CollisionFlags.CollidedBelow) != 0;

Mainly the second line is a problem. What does the &-mark do, and where is the !=0 for? And for the first line: an collision is equal to a movement? does that make sence? Can someone explain this in meta language?

I hope some of you can help me out. tnx.

PS: this is indeed the basic CharacterMovement script, edited a bit here and there.

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

1 Reply

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

Answer by Michael La Voie · Mar 26, 2010 at 04:28 PM

Flags are a special type of variable harking back to the nature of how memory is stored: in binary. Fortunately, you don't need to understand the why to understand how they work.

A flag variable holds one or more bit/bool (true/false) values. Each CharacterController has a CharacterController.collisionFlags flag variable. This flag lets you know if you controller has collided with any other object and also where it collided. It has four bool values:

  • none
  • sides
  • above
  • below

Although a flag's bit values can be true or false in any combination, the controller will prevent "none" from being combined with any others because that doesn't make sense. However, it does make sense that you could collide with sides AND below, such as if you're on the ground and leaning against a wall.

As for its relation to Move(), the function returns this variable (which you can also always get through CharacterController.collisoinFlags) as a matter of convenience. Commonly, after moving your character, you need to know if it just hit something.

The & Syntax

You also asked about the syntax and what it means in plain English.

grounded = (flags & CollisionFlags.CollidedBelow) != 0;

For starters, when comparing flags you are required to use "&" to request any flags that are shared between two flags and "|" to request any flags that either of the two have.

The statement above is asking first to see which flags they have in common. The flags variable can have any of the four flags I mentioned set. The CollisionFlags.CollidedBelow only has the below flag set. When compared to see what they have in common, it'll either return "none" (which is also represented by zero) or "below". You could also write it as so:

bool grounded = (flags & CollisionFlags.CollidedBelow);
CollisionFlags myFlags = (flags & CollisionFlags.CollidedBelow);
grounded = myFlags == CollisionFlags.CollidedBelow;
grounded = myflags != CollisionFlags.None;

See this SO question for a technical explanation.

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

No one has followed this question yet.

Related Questions

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

Camera Recoil,Please help me. 2 Answers

C# How to have weapon pickup 1 Answer

Unity3D HELP! How can i pickup/hold/throw object when im rolling ball? 4 Answers

Input.GetAxis won't start working properly. 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