没有按预期循环通过阵列

时间:2019-01-07 01:41:29

标签: c# unity3d

我试图让player在他跳动后继续(请参见代码),但是他所做的是返回jumpPosition变换而没有继续进行下一个操作。 / p>

这是Unity中的工作:

enter image description here  ...

这是我的playerMovment代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class MovementController : MonoBehaviour
{

    // public GameObject playerToMove;           // not sure why I need this 

    public float moveSpeed;                     // move speed of the player going from player postion to current point, possible to use somewhere else

    private Transform currentPoint;             // used to determine where the next point the player has to move by cycling through 'points' array
    public Transform jumpPoint;                 // used as a location trigger to tell the player when to jump -- will be attempting to make into an array
    public Transform crouchPoint;               // used as a location trigger to tell the player when to crounch -- will be attempting to make into an array
    public Transform[] points;                  // an array of location for the 'currentPoint' to cycle through 

    public float maxPause = 100;                // used to determine the length of time between when the player arrives at the 'currentPoint' and when to leave said point; default = 100
    public float reducedPause = 2;                // used to set 'maxPause' to a smaller number so that player won't keep jumping/crouching

    public TestCharacterController2D controller; // acceses the TestCharacterController2D script (I didnt write this script but plan to modiify) used for basic move, jump, and crouch funtions 

    public Animator animator;                   // my attempt to find the player's animator

    public bool isRight;                        // used to to determine which way the character is facing -- I think this can be accesed through the 'controller' variable (TestCharacterController2D script)

    private bool jump;                          // to tell the 'controller' when to jump
    private bool crouch;                        // to tell the 'controller' when to crouch
    private bool pause = false;                 // used to determine when the player arrives at the 'currentPoint' and the 'maxPause' countdown begins

    public int pointsSelection;                 // used to cycle the 'points' array when maxPause cycle is over and player is at current point

    //   public float jumpHeight = 100f;             // not sure why used


    void Start()                                         // looking into 'onAwake' maybe? (or others)
    {
        currentPoint = points[pointsSelection];         // sets currentPoint to default location ('pointSelection' is 'publc' so can be modified in Unity


        isRight = true;                                 // player starts facing right -- as per character animations

    }

    void Update() // not sure if should have more in 'FixedUpdate' or others (maybe?)
    {
        jump = false;

        if (Vector2.Distance(transform.position, currentPoint.position) < 0.05f)
        // checks to see if player is at 'currentPoint' 
        {
            pause = true; // starts the pause sequenece
            Debug.Log("Pause = " + pause);

            if (pause) // when the movement is pause do the the following
            {
                moveSpeed = 0;

                animator.SetFloat("Speed", 0); // player stops moving -- works!

                if (maxPause <= 100) // checks to see if still paused
                {

                    Debug.Log("this is maxPause: " + maxPause);

                    if (maxPause < 0) // found 'maxPause' was going to far below zero
                        maxPause = 0;

                    maxPause--; // reduce pause amount (working way out of loop)

                }



                if (maxPause == 0) // when 'maxPause' timer has finished
                {
                    pointsSelection++; // move to next point
                    maxPause = 100; // reset 'maxPause' timer
                    pause = false; // resume 'transform.position == currentPoint.position' process
                }

            }


            if (pointsSelection == points.Length) // makes sure 'pointsSelection' doesn't go out of bounds
            {
                Debug.Log("at end of array");
                pointsSelection = 0; // start the player's movement process over again
            }

        }
        else // not sure if requried
        {
            Debug.Log("pause = false");
            Debug.Log("this is the moveSpeed " + moveSpeed);
            Debug.Log("pointsSelection: " + pointsSelection);
        }

        if (Vector2.Distance(transform.position, jumpPoint.position) < 0.05f && jumpPoint == currentPoint) // conditions for the jump action (automatic) -- I fell the whole thing needs to be more elaborate ** WORK IN PROGRESS **
        {
            jump = true;
        }
        else
            jump = false;

        currentPoint = points[pointsSelection]; // moved to line 130 -- not sure if better here


    }

    void FixedUpdate()
    {

        if (isRight && transform.position.x > currentPoint.position.x) // flipping the character -- I'm pretty sure I can use TestCharacterController2D to do this for me, this is comparing the player's 'transform' 
        {
            moveSpeed = -0.25f; // tells controller to head in the left direction
            isRight = false;   // no longer facing right 
        }
        if (!isRight && transform.position.x < currentPoint.position.x) // reverse of above
        {
            moveSpeed = 0.25f; // tells controller to head in the right direction
            isRight = true; // no longer facing left
        }

        if (moveSpeed > 0 || moveSpeed < 0)
            animator.SetFloat("Speed", 1); // player starts PlayerRun animation -- works!


        // Move our character
        controller.Move(moveSpeed, crouch, jump); // draws from the TestCharacterController2D script

    }


    public void OnLanding()
    {
        animator.SetBool("Jumped", false);
    }

}

0 个答案:

没有答案