Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sdgd · Oct 13, 2013 at 11:40 AM · c#noobdeclarationnot-enough-information

variable that's not declared works without error why? - How Does property - method work (Get Set)

I'm learning the locomotion and this script bugges me:

PlatformCharacterController script changes

desiredMovementDirection in this script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 [RequireComponent(typeof(CapsuleCollider))]
 public class PhysicsCharacterMotor : CharacterMotor {
     
     public float maxRotationSpeed = 270;
     public bool useCentricGravity = false;
     public LayerMask groundLayers;
     public Vector3 gravityCenter = Vector3.zero;
     
     void Awake () {
         rigidbody.freezeRotation = true;
         rigidbody.useGravity = false;
     }
     
     private void AdjustToGravity() {
         int origLayer = gameObject.layer;
         gameObject.layer = 2;
         
         Vector3 currentUp = transform.up;
         //Vector3 gravityUp = (transform.position-gravityCenter).normalized;
         
         float damping = Mathf.Clamp01(Time.deltaTime*5);
         
         RaycastHit hit;
         
         Vector3 desiredUp = Vector3.zero;
         for (int i=0; i<8; i++) {
             Vector3 rayStart =
                 transform.position
                     + transform.up
                     + Quaternion.AngleAxis(360*i/8.0f, transform.up)
                         * (transform.right*0.5f)
                     + desiredVelocity*0.2f;
             if ( Physics.Raycast(rayStart, transform.up*-2, out hit, 3.0f, groundLayers.value) ) {
                 desiredUp += hit.normal;
             }
         }
         desiredUp = (currentUp+desiredUp).normalized;
         Vector3 newUp = (currentUp+desiredUp*damping).normalized;
         
         float angle = Vector3.Angle(currentUp,newUp);
         if (angle>0.01) {
             Vector3 axis = Vector3.Cross(currentUp,newUp).normalized;
             Quaternion rot = Quaternion.AngleAxis(angle,axis);
             transform.rotation = rot * transform.rotation;
         }
         
         gameObject.layer = origLayer;
     }
     
     private void UpdateFacingDirection() {
         // Calculate which way character should be facing
         float facingWeight = desiredFacingDirection.magnitude;
         Vector3 combinedFacingDirection = (
             transform.rotation * desiredMovementDirection * (1-facingWeight)
             + desiredFacingDirection * facingWeight
         );
         combinedFacingDirection = Util.ProjectOntoPlane(combinedFacingDirection, transform.up);
         combinedFacingDirection = alignCorrection * combinedFacingDirection;
         
         if (combinedFacingDirection.sqrMagnitude > 0.1f) {
             Vector3 newForward = Util.ConstantSlerp(
                 transform.forward,
                 combinedFacingDirection,
                 maxRotationSpeed*Time.deltaTime
             );
             newForward = Util.ProjectOntoPlane(newForward, transform.up);
             //Debug.DrawLine(transform.position, transform.position+newForward, Color.yellow);
             Quaternion q = new Quaternion();
             q.SetLookRotation(newForward, transform.up);
             transform.rotation = q;
         }
     }
     
     private void UpdateVelocity() {
         Vector3 velocity = rigidbody.velocity;
         if (grounded) velocity = Util.ProjectOntoPlane(velocity, transform.up);
         
         // Calculate how fast we should be moving
         jumping = false;
         if (grounded) {
             // Apply a force that attempts to reach our target velocity
             Vector3 velocityChange = (desiredVelocity - velocity);
             if (velocityChange.magnitude > maxVelocityChange) {
                 velocityChange = velocityChange.normalized * maxVelocityChange;
             }
             rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
         
             // Jump
             if (canJump && Input.GetButton("Jump")) {
                 rigidbody.velocity = velocity + transform.up * Mathf.Sqrt(2 * jumpHeight * gravity);
                 jumping = true;
             }
         }
         
         // Apply downwards gravity
         rigidbody.AddForce(transform.up * -gravity * rigidbody.mass);
         
         grounded = false;
     }
     void OnCollisionStay () {
         grounded = true;
     }
     
     void FixedUpdate () {
         if (useCentricGravity) AdjustToGravity();
         
         UpdateFacingDirection();
         
         UpdateVelocity();
     }
     
 }

and I don't understand how can it change it if it's not declated it's not public nor even private

I did Ctrl + F and found only and only 1 through whole script.

this bugges my whole basic understanding of programming.

I need some explanations here thanks.

PS. what am I doing: I don't know --> (learning and combining my programmed stuff with locomotion).

I changed the title since it was clearly explained how property works

Comment
Add comment · Show 12
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 Hoeloe · Oct 13, 2013 at 11:43 AM 2
Share

Is desired$$anonymous$$ovementDirection declared in the Character$$anonymous$$otor class?

avatar image fafase · Oct 13, 2013 at 11:57 AM 1
Share

Did you try right-click Go To Definition ?

avatar image Hoeloe · Oct 13, 2013 at 12:02 PM 1
Share

It's not a function, it's a property.

avatar image fafase · Oct 13, 2013 at 12:06 PM 1
Share

it is a property, that is why it looks like a variable.

A property is a .NET feature that looks like Getter/Setter from Java. It looks like a variable but works like a method.

You have a private variable that you encapsulate using a property. That allows the developer to prevent wrong use from the client.

In this case, the client (you, the user) cannot pass a vector with magnitude less than 1. if so, it is corrected.

This is because the rest of the application relies on the fact that this is always at least 1.

avatar image Hoeloe · Oct 13, 2013 at 01:17 PM 2
Share

You missed a couple of lines out in your example property:

 public decimal deposit
 {
     set
     {
         if(value<0) _deposit = 0;
         else _deposit = value;
     }
 }

Note that this property only has a set subsection - making it a write-only property. This is unusual, as it means you can set, but not read the value, and this has very few uses. However, by doing the reverse and only having a get subsection (without set), you create a read-only property, which is very useful, as it allows you to look at a value, but does not provide you with the ability to alter it externally from the class.

Show more comments

1 Reply

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

Answer by Tomer-Barkan · Oct 13, 2013 at 12:13 PM

Your class inherits from CharacterMotor. If desiredMovementDirection is public in CharacterMotor, it will also be public in your class.

C# is type safe so the only possibility is that the variable is defined in one of it's parent classes.

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

17 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Respawn Die Script (C#) 1 Answer

Controlling world gravity via object's y axis ; transform.up = -Physics.gravity in C# ? 1 Answer

Unfixable Assets/Scripts/PlayerAttack.cs(30,15): error CS1525: Unexpected symbol `private' 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