使用C#和Unity在数组中循环不断地移动“玩家”

时间:2019-01-06 21:00:58

标签: c# unity3d

我试图让“玩家”连续循环移动我点中的所有点(变换)[]

由于某些原因,当“玩家”到达“ pointsSelection” = 3时,脚本将停留在该位置,并且不会从此处继续

我认为这可能很简单,但我无法弄清楚

(如果有任何Unity设置,您需要查看,我可以发布其屏幕截图,让我知道)

这是我到目前为止所拥有的:

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?)
    {
        MoveDirection();

        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 - NOT SURE WHILE BUT SCRIPT STICKS HERE AT 'pointsSelection' = 3 and 'moveSpeed' = 0
        {
            Debug.Log("pause = false");
            Debug.Log("this is the moveSpeed " + moveSpeed);
            Debug.Log("pointsSelection: " + pointsSelection);
        }

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



        /*      if (pause && transform.position == jumpPoint.position && !jump) // conditions for the jump action (automatic) -- I fell the whole thing needs to be more elaborate ** WORK IN PROGRESS **
             {
                 jump = true;


             }
             else  
                 jump = false;

            if (pause && transform.position == crouchPoint.position) // I feel statement needs more elaboration ** WORK IN PROGRESS **
             { 
                 crouch = true;


             }
             else
                 crouch = false;
                 */
        if (!pause && currentPoint == jumpPoint && maxPause == 100) // only way I could figure out how to set 'maxPause' to 'reducedPause' while still breaking the if statement
        {
            maxPause = reducedPause; // reduces the wait time when jumping -- may need some tweeking

        }

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


    }

    void FixedUpdate()
    {



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

    }

    public void MoveDirection()
    {
        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)
            animator.SetFloat("Speed", 1); // player starts PlayerRun animation -- works!
    }


}

任何帮助将不胜感激

谢谢

littlejiver

1 个答案:

答案 0 :(得分:0)

它知道这很简单,一旦我移动了最后一个转换,脚本就可以正常运行,我相信转换之间的距离太近了