Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     void Update()
     {
         if (isControllable)
         {
             if (Input.GetButtonDown("Jump"))
             {
                 lastJumpButtonTime = Time.time;
             }

             UpdateSmoothedMovementDirection();

             // Apply gravity
             // - extra power jump modifies gravity
             // - controlledDescent mode modifies gravity
             ApplyGravity();

             // Apply jumping logic
             ApplyJumping();


             // Calculate actual motion
             Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;
             movement *= Time.deltaTime;

             //Debug.Log(movement.x.ToString("0.000") + ":" + movement.z.ToString("0.000"));

             // Move the controller
             CharacterController controller = GetComponent();
             collisionFlags = controller.Move(movement);

         }

         // PUN: if a remote position is known, we smooth-move to it (being late(r) but smoother)
         if (this.remotePosition != Vector3.zero)
         {
             transform.position = Vector3.Lerp(transform.position, this.remotePosition, Time.deltaTime * this.RemoteSmoothing);
         }

         velocity = (transform.position - lastPos)*25;

         // ANIMATION sector
         if (_animation)
         {
             if (_characterState == PickupCharacterState.Jumping)
             {
                 if (!jumpingReachedApex)
                 {
                     _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
                     _animation.CrossFade(jumpPoseAnimation.name);
                 }
                 else
                 {
                     _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
                     _animation.CrossFade(jumpPoseAnimation.name);
                 }
             }
             else
             {
                 if (_characterState == PickupCharacterState.Idle)
                 {
                     _animation.CrossFade(idleAnimation.name);
                 }
                 else if (_characterState == PickupCharacterState.Running)
                 {
                     _animation[runAnimation.name].speed = runMaxAnimationSpeed;
                     if (this.isControllable)
                     {
                         _animation[runAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, runMaxAnimationSpeed);
                     }
                     _animation.CrossFade(runAnimation.name);
                 }
                 else if (_characterState == PickupCharacterState.Trotting)
                 {
                     _animation[walkAnimation.name].speed = trotMaxAnimationSpeed;
                     if (this.isControllable)
                     {
                         _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
                     }
                     _animation.CrossFade(walkAnimation.name);
                 }
                 else if (_characterState == PickupCharacterState.Walking)
                 {
                     _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
                     if (this.isControllable)
                     {
                         _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
                     }
                     _animation.CrossFade(walkAnimation.name);
                 }

                 if (_characterState != PickupCharacterState.Running)
                 {
                     _animation[runAnimation.name].time = 0.0f;
                 }
             }
         }
         // ANIMATION sector

         // Set rotation to the move direction
         if (IsGrounded())
         {
             // a specialty of this controller: you can disable rotation!
             if (DoRotate)
             {
                 transform.rotation = Quaternion.LookRotation(moveDirection);
             }
         }
         else
         {
             /* This causes choppy behaviour when colliding with SIDES
              * Vector3 xzMove = velocity;
             xzMove.y = 0;
             if (xzMove.sqrMagnitude > 0.001f)
             {
                 transform.rotation = Quaternion.LookRotation(xzMove);
             }*/
         }

         // We are in jump mode but just became grounded
         if (IsGrounded())
         {
             lastGroundedTime = Time.time;
             inAirVelocity = Vector3.zero;
             if (jumping)
             {
                 jumping = false;
                 SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
             }
         }

         lastPos = transform.position;
     }