- Home /
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
Is desired$$anonymous$$ovementDirection
declared in the Character$$anonymous$$otor class?
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.
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.
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.
Your answer
Follow this Question
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