예제 #1
0
 void Start()
 {
     if (character == null)
     {
         character = GetComponentInParent <CharacterMove>();
     }
     if (character == null)
     {
         character = GetComponent <CharacterMove>();
     }
     if (character == null)
     {
         Utility.Follow f = GetComponent <Utility.Follow>();
         if (f)
         {
             character = f.whoToFollow.GetComponent <CharacterMove>();
         }
     }
     if (character != null)
     {
         character.callbacks.moveDirectionChanged.AddListener(callbacks.moveDirectionChanged.Invoke);
         character.callbacks.stand.AddListener(callbacks.stand.Invoke);
         character.callbacks.jumped.AddListener(callbacks.jumped.Invoke);
         character.callbacks.fall.AddListener(callbacks.fall.Invoke);
         character.callbacks.wallCollisionStart.AddListener(callbacks.wallCollisionStart.Invoke);
         character.callbacks.wallCollisionStopped.AddListener(callbacks.wallCollisionStopped.Invoke);
         character.callbacks.arrived.AddListener(callbacks.arrived.Invoke);
     }
 }
 private void Start()
 {
     if (animator == null)
     {
         animator = GetComponent <Animator>();
     }
     if (animator == null)
     {
         animator = GetComponentInParent <Animator>();
     }
     if (character == null)
     {
         character = GetComponent <CharacterMove>();
     }
     if (character == null)
     {
         character = GetComponentInParent <CharacterMove>();
     }
     if (character == null)
     {
         Utility.Follow f = GetComponent <Utility.Follow>();
         if (f)
         {
             character = f.whoToFollow.GetComponent <CharacterMove>();
         }
     }
     character.callbacks.jumped.AddListener(Jump);
     character.callbacks.stand.AddListener(Stand);
     character.callbacks.fall.AddListener(Fall);
     character.callbacks.arrived.AddListener(Wave);
 }
예제 #3
0
 public void FixedUpdate(CharacterMove c)
 {
     if (isStableOnGround || canMoveInAir)
     {
         ApplyMoveFromInput(c);
     }
 }
예제 #4
0
            public void FixedUpdate(CharacterMove cm)
            {
                if (!enabled)
                {
                    return;
                }
                bool  peakedAtStart = peaked, jumpingAtStart = isJumping;
                bool  jpress = pressed;
                ulong now    = Proc.Now;

                if (TimedJumpPress > 0)
                {
                    jpress = true; TimedJumpPress -= Time.deltaTime; if (TimedJumpPress < 0)
                    {
                        TimedJumpPress = 0;
                    }
                }
                bool  lateButForgiven = false;
                ulong late            = 0;

                if (cm.move.isStableOnGround)
                {
                    usedDoubleJumps = 0;
                }
                else if (jpress && forgiveLateJumps && (late = Proc.Now - stableTime) < jumpLagForgivenessMs)
                {
                    stableTime = 0;
                    cm.move.isStableOnGround = lateButForgiven = true;
                }
                if (jpress && cm.move.isStableOnGround && !isJumping && now - timePressed < jumpTooEarlyForgivenessMs)
                {
                    timePressed = 0;
                    if (!lateButForgiven)
                    {
                        Start(cm.body.position);
                    }
                    else
                    {
                        Start(position); jumpTime -= late;
                    }
                }
                float gForce = Mathf.Abs(Physics.gravity.y);

                if (isJumping)
                {
                    Vector3 vel = cm.rb.velocity;
                    JumpUpdate(now, gForce, cm.move.speed, jpress, cm.body, ref vel);
                    cm.rb.velocity = vel;
                }
                else
                {
                    peaked = now >= peakTime;
                }
                if (!isJumping && jpress && usedDoubleJumps < doubleJumps)
                {
                    DoubleJump(cm.body, cm.move.speed, gForce, peaked && !peakedAtStart && jumpingAtStart);
                }
            }
예제 #5
0
 public void FixedUpdate(CharacterMove c)
 {
     if (isStableOnGround || canMoveInAir)
     {
         if (isStableOnGround)
         {
             c.jump.MarkStableJumpPoint(c.body.position);
         }
         ApplyMoveFromInput(c);
     }
 }
예제 #6
0
 public void Init()
 {
     if (move == null)
     {
         move = GetComponentInChildren <CharacterMove>();
     }
     if (data == null)
     {
         data = GetComponentInChildren <ScriptedDictionary>();
     }
 }
예제 #7
0
        public ClickToMoveFollower Follower(CharacterMove currentChar)
        {
            ClickToMoveFollower follower = currentChar.GetComponent <ClickToMoveFollower>();

            if (follower == null)
            {
                follower = currentChar.gameObject.AddComponent <ClickToMoveFollower>();
                follower.clickToMoveUi = this;
                ParticleSystem ps = currentChar.GetComponentInChildren <ParticleSystem>();
                if (ps != null)
                {
                    ParticleSystem.MainModule m = ps.main;
                    follower.color = m.startColor.color;
                }
                follower.Init(currentChar.gameObject);
            }
            return(follower);
        }
예제 #8
0
 /// called when created by Unity Editor
 void Reset()
 {
     if (target == null)
     {
         CharacterMove body = null;
         if (body == null)
         {
             body = transform.GetComponentInParent <CharacterMove>();
         }
         if (body == null)
         {
             body = FindObjectOfType <CharacterMove>();
         }
         if (body != null)
         {
             target = body.head;
         }
     }
 }
예제 #9
0
            /// <summary>
            ///
            /// </summary>
            /// <param name="cm"></param>
            /// <param name="collision"></param>
            /// <returns>the index of collision that could cause stability</returns>
            public int CollisionStabilityCheck(CharacterMove cm, Collision collision)
            {
                float   biggestOpposition = -Vector3.Dot(moveDirection, oppositionDirection);
                int     stableIndex = -1, wallCollisions = -1;
                Vector3 standingNormal = Vector3.zero;

                // identify that the character is on the ground if it's colliding with something that is angled like ground
                for (int i = 0; i < collision.contacts.Length; ++i)
                {
                    Vector3 surfaceNormal = collision.contacts[i].normal;
                    float   a             = Vector3.Angle(Vector3.up, surfaceNormal);
                    if (a <= maxStableAngle)
                    {
                        isStableOnGround = true;
                        stableIndex      = i;
                        standingNormal   = surfaceNormal;
                    }
                    else
                    {
                        float opposition = -Vector3.Dot(moveDirection, surfaceNormal);
                        if (opposition > biggestOpposition)
                        {
                            biggestOpposition   = opposition;
                            wallCollisions      = i;
                            oppositionDirection = surfaceNormal;
                        }
                        if (automaticMovement.jumpAtObstacle)
                        {
                            cm.jump.TimedJumpPress = cm.jump.fullPressDuration;
                        }
                    }
                }
                if (wallCollisions != -1)
                {
                    if (lastOppositionDirection != oppositionDirection)
                    {
                        cm.callbacks.wallCollisionStart.Invoke(oppositionDirection);
                    }
                    lastOppositionDirection = oppositionDirection;
                }
                return(stableIndex);
            }
예제 #10
0
 /// called when created by Unity Editor
 void Reset()
 {
     if (characterToMove == null)
     {
         characterToMove = transform.GetComponentInParent <CharacterMove>();
     }
     if (characterToMove == null)
     {
         characterToMove = FindObjectOfType <CharacterMove>();
     }
     if (_camera == null)
     {
         _camera = GetComponent <Camera>();
     }
     if (_camera == null)
     {
         _camera = Camera.main;
     }
     if (_camera == null)
     {
         _camera = FindObjectOfType <Camera>();;
     }
 }
예제 #11
0
        public void Init(GameObject go)
        {
            if (mover != null)
            {
                return;
            }
            mover = GetComponent <CharacterMove>();
            CapsuleCollider cap = mover.GetComponent <CapsuleCollider>();

            if (cap != null)
            {
                characterHeight = cap.height / 2;
                characterRadius = cap.radius;
            }
            else
            {
                characterHeight = characterRadius = 0;
            }
            if (line == null)
            {
                line = Lines.MakeWire();
            }
            line.Line(Vector3.zero);
        }
예제 #12
0
 public void SetFollower(CharacterMove target)
 {
     follower = Follower(target);
 }
예제 #13
0
            public void ApplyMoveFromInput(CharacterMove cm)
            {
                Vector3   moveVelocity = Vector3.zero;
                Transform t            = cm.body;
                Vector3   oldDirection = moveDirection;

                moveDirection = new Vector3(strafeRightMovement, 0, moveForwardMovement);
                float intendedSpeed = 1;

                if (moveDirection != Vector3.zero)
                {
                    moveDirection = ConvertIntentionToRealDirection(moveDirection, t, out intendedSpeed);
                    if (intendedSpeed > 1)
                    {
                        intendedSpeed = 1;
                    }
                    // else { Debug.Log(intendedSpeed); }
                }
                if (automaticMovement.enabled)
                {
                    if (moveDirection == Vector3.zero)
                    {
                        if (!automaticMovement.arrived)
                        {
                            moveDirection = automaticMovement.CalculateMoveDirection(t.position, speed * intendedSpeed, Vector3.up, ref automaticMovement.arrived);
                            if (automaticMovement.arrived)
                            {
                                cm.callbacks.arrived.Invoke(automaticMovement.targetPosition);
                            }
                        }
                    }
                    else
                    {
                        automaticMovement.arrived = true;                         // if the player is providing input, stop calculating automatic movement
                    }
                }
                if (moveDirection != Vector3.zero)
                {
                    moveVelocity = AccountForBlocks(moveDirection);
                    // apply the direction-adjusted movement to the velocity
                    moveVelocity *= (speed * intendedSpeed);
                }
                if (moveDirection != oldDirection)
                {
                    cm.callbacks.moveDirectionChanged.Invoke(moveDirection);
                }
                float gravity = cm.rb.velocity.y;         // get current gravity

                moveVelocity.y = gravity;                 // apply to new velocity
                if (lookForwardMoving && moveDirection != Vector3.zero && orientationTransform != null)
                {
                    cm.body.rotation = Quaternion.LookRotation(moveDirection, Vector3.up);
                    if (cm.head != null)
                    {
                        cm.head.localRotation = Quaternion.identity;
                    }                                                                                        // turn head straight while walking
                }
                if (!systemMovement)
                {
                    cm.rb.velocity = moveVelocity;
                }
                else
                {
                    cm.body.position += moveVelocity * Time.unscaledDeltaTime;
                }
                lastVelocity = moveVelocity;
                if (oppositionDirection == Vector3.zero && lastOppositionDirection != Vector3.zero)
                {
                    cm.callbacks.wallCollisionStopped.Invoke();                     // done colliding
                    lastOppositionDirection = Vector3.zero;
                }
                oppositionDirection = Vector3.zero;
            }
예제 #14
0
            /// <summary>pretends to hold the jump button for the specified duration</summary>
            public void FixedUpdate(CharacterMove p)
            {
                if (inputHeld = (PressJump > 0))
                {
                    PressJump -= Time.deltaTime;
                }
                if (impulseActive && !inputHeld)
                {
                    impulseActive = false; peaked = true;
                }
                if (!inputHeld)
                {
                    return;
                }
                bool isStableOnGround = p.IsStableOnGround();

                // check stable footing for the jump
                if (isStableOnGround && !impulseActive)
                {
                    jumpsSoFar          = 0;
                    heightReached       = 0;
                    currentJumpVelocity = 0;
                    timeHeld            = 0;
                    peaked = true;                     // used for multi-jumping state
                }
                // calculate the jump
                float   gForce = -Physics.gravity.y * p.rb.mass;
                Vector3 jump_force = Vector3.zero, jumpDirection = Vector3.up;                //-p.gravity.dir;

                // if the user wants to jump, and is allowed to jump again
                if (!impulseActive && (jumpsSoFar < maxJumps) && peaked)
                {
                    heightReached = 0;
                    timeHeld      = 0;
                    jumpsSoFar++;
                    targetHeight = minJumpHeight * p.rb.mass;
                    float velocityRequiredToJump = Mathf.Sqrt(targetHeight * 2 * gForce);
                    // cancel out current jump/fall forces
                    if (jumpStartResetsVerticalMotion)
                    {
                        float motionInVerticalDirection = Vector3.Dot(jumpDirection, p.rb.velocity);
                        jump_force -= (motionInVerticalDirection * jumpDirection) / Time.deltaTime;
                    }
                    // apply proper jump force
                    currentJumpVelocity = velocityRequiredToJump;
                    peaked              = false;
                    jump_force         += (jumpDirection * currentJumpVelocity) / Time.deltaTime;
                    impulseActive       = true;
                    p.move.groundNormal = jumpDirection;                     // animation callback code might be waiting for this
                    p.callbacks.jumped.Invoke(jumpDirection);
                }
                else
                // if a jump is happening
                if (currentJumpVelocity > 0)
                {
                    // handle jump height: the longer you hold jump, the higher you jump
                    if (inputHeld)
                    {
                        timeHeld += Time.deltaTime;
                        if (timeHeld >= fullJumpPressDuration)
                        {
                            targetHeight = maxJumpHeight;
                            timeHeld     = fullJumpPressDuration;
                        }
                        else
                        {
                            targetHeight  = minJumpHeight + ((maxJumpHeight - minJumpHeight) * timeHeld / fullJumpPressDuration);
                            targetHeight *= p.rb.mass;
                        }
                        if (heightReached < targetHeight)
                        {
                            float requiredJumpVelocity = Mathf.Sqrt((targetHeight - heightReached) * 2 * gForce);
                            float forceNeeded          = requiredJumpVelocity - currentJumpVelocity;
                            jump_force         += (jumpDirection * forceNeeded) / Time.deltaTime;
                            currentJumpVelocity = requiredJumpVelocity;
                        }
                    }
                    else
                    {
                        peaked = true;
                    }
                }
                else
                {
                    impulseActive = false;
                }
                if (currentJumpVelocity > 0)
                {
                    float moved = currentJumpVelocity * Time.deltaTime;
                    heightReached       += moved;
                    heightReachedTotal  += moved;
                    currentJumpVelocity -= gForce * Time.deltaTime;
                }
                else if (!peaked && !isStableOnGround)
                {
                    peaked        = true;
                    impulseActive = false;
                }
                p.rb.AddForce(jump_force);
            }