コード例 #1
0
    private void Start()
    {
        originalGravity = basicMovementController.currentParameters.gravity;
        GravityActive(false);

        playerMovement = GameObject.FindWithTag("Player").GetComponent <BasicMovementController>();
    }
コード例 #2
0
    private void HorizontalMovement(float horizontalMove)
    {
        float normalizedHorizontalSpeed = 0;

        // If the value of the horizontal axis is positive, the character must face right.
        if (horizontalMove > 0.1f)
        {
            normalizedHorizontalSpeed = horizontalMove;
        }
        // If it's negative, then we're facing left
        else if (horizontalMove < -0.1f)
        {
            normalizedHorizontalSpeed = horizontalMove;
        }
        else
        {
            normalizedHorizontalSpeed = 0;
        }

        currentHSpeed = normalizedHorizontalSpeed * hMovementSpeed;
        basicMovementController.SetHorizontalForce(currentHSpeed);

        if (!playerMovement)
        {
            playerMovement = GameObject.FindWithTag("Player").GetComponent <BasicMovementController>();
        }

        if (playerMovement.standingOn == this.gameObject &&
            playerMovement.basicMovementState.isGrounded)
        {
            playerMovement.SetHorizontalForce(currentHSpeed);
        }
    }
コード例 #3
0
    // Use this for initialization
    void Start()
    {
        playerBattleManager       = GameObject.Find("PlayerBattleManager");
        enemyBattleManager        = GameObject.Find("EnemyBattleManager");
        playerBattleManagerScript = playerBattleManager.GetComponent <PlayerBattleManager>();
        enemyBattleManagerScript  = enemyBattleManager.GetComponent <EnemyBattleManager>();
        myStats        = gameObject.GetComponent <UnitStats>();
        moveController = gameObject.GetComponent <BasicMovementController>();
        targetsList    = new List <GameObject>();

        if (myStats.unitName == "Turret")
        {
            turret = GetComponent <Turret>();
        }

        if (myStats.unitName != "Turret")
        {
            if (gameObject.tag == "PlayerUnit")
            {
                enemyGenerator = GameObject.Find("EnemyGenerator");
                targetsList.Add(enemyGenerator);
            }
            else
            {
                enemyGenerator = GameObject.Find("PlayerGenerator");
                targetsList.Add(enemyGenerator);
            }
        }
    }
コード例 #4
0
    public void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log(other.name + " has entered the grass.");
        BasicMovementController bmc = other.GetComponent <BasicMovementController>();

        bmc.setInGrass(true);
    }
コード例 #5
0
    /// <summary>
    ///Initializes this instance of the character
    /// </summary>
    void Awake()
    {
        advancedMovementState            = new AdvancedMovementState();
        basicMovementController          = GetComponent <BasicMovementController>();
        currentParameters.hMovementSpeed = currentParameters.walkSpeed;

        //_sceneCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraController>();
    }
コード例 #6
0
    private void SelectAndMoveUnit()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Physics2D.queriesHitTriggers = false;
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            Physics2D.queriesHitTriggers = true;

            if (hit.collider != null && !hit.transform.gameObject.name.Contains("Turret"))
            {
                if (hit.collider.transform.gameObject.tag == "PlayerUnit")
                {
                    if (selectedUnit != null && selectedUnit != hit.collider.transform.gameObject)
                    {
                        GameObject     newChildSprite  = selectedUnit.transform.GetChild(0).gameObject;
                        SpriteRenderer newChangeSprite = newChildSprite.GetComponent <SpriteRenderer>();
                        Color          newAlpha        = newChangeSprite.color;
                        newAlpha.a            = 0;
                        newChangeSprite.color = newAlpha;
                    }

                    selectedUnit = hit.collider.transform.gameObject;
                    GameObject     childSprite  = selectedUnit.transform.GetChild(0).gameObject;
                    SpriteRenderer changeSprite = childSprite.GetComponent <SpriteRenderer>();
                    Color          alpha        = changeSprite.color;
                    alpha.a                    = 255;
                    changeSprite.color         = alpha;
                    defendAreaSprite.enabled   = true;
                    defendAreaCollider.enabled = true;
                }
                else if (hit.transform.gameObject.name == "PlayerDefendArea")
                {
                    if (selectedUnit != null)
                    {
                        BasicMovementController unitMoveController = selectedUnit.GetComponent <BasicMovementController>();
                        Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                        newPosition.z = -1;
                        unitMoveController.newDefendPosition = newPosition;
                        unitMoveController.haveNewPosition   = true;
                    }
                }
            }
            else
            {
                if (selectedUnit != null && hit.transform == null)
                {
                    GameObject     childSprite  = selectedUnit.transform.GetChild(0).gameObject;
                    SpriteRenderer changeSprite = childSprite.GetComponent <SpriteRenderer>();
                    Color          alpha        = changeSprite.color;
                    alpha.a                    = 0;
                    changeSprite.color         = alpha;
                    selectedUnit               = null;
                    defendAreaSprite.enabled   = false;
                    defendAreaCollider.enabled = false;
                }
            }
        }
    }
コード例 #7
0
    /// <summary>
    /// If we're in the air and moving up, we cast rays above the character's head to check for collisions
    /// </summary>
    private void CastRaysAbove()
    {
        if (basicMovementState.isGrounded)
        {
            return;
        }

        float rayLength = basicMovementState.isGrounded?rayOffset : newPosition.y * Time.deltaTime;

        rayLength += rayBoundsRectangle.height / 2;

        bool hitConnected = false;

        Vector2 verticalRayCastStart = new Vector2(rayBoundsRectangle.xMin + newPosition.x,
                                                   rayBoundsRectangle.center.y);
        Vector2 verticalRayCastEnd = new Vector2(rayBoundsRectangle.xMax + newPosition.x,
                                                 rayBoundsRectangle.center.y);

        RaycastHit2D[] hitsStorage      = new RaycastHit2D[numberOfVerticalRays];
        float          smallestDistance = largeValue;

        for (int i = 0; i < numberOfVerticalRays; i++)
        {
            Vector2 rayOriginPoint = Vector2.Lerp(verticalRayCastStart, verticalRayCastEnd, (float)i / (float)(numberOfVerticalRays - 1));
            hitsStorage[i] = BasicMovementController.RayCast(rayOriginPoint, Vector2.up, rayLength, platformMask & ~edgeColliderPlatformMask, true, Color.green);


            if (hitsStorage[i])
            {
                hitConnected = true;
                if (hitsStorage[i].distance < smallestDistance)
                {
                    smallestDistance = hitsStorage[i].distance;
                }
            }
        }

        if (hitConnected)
        {
            _speed.y      = 0;
            newPosition.y = smallestDistance - rayBoundsRectangle.height / 2;

            basicMovementState.isCollidingAbove = true;

            if (!basicMovementState.wasTouchingTheCeilingLastFrame)
            {
                newPosition.x = 0;
                _speed        = new Vector2(0, _speed.y);
            }
        }
    }
コード例 #8
0
    /// <summary>
    /// コンストラクタ
    /// </summary>
    void Start()
	{
		BMController = GetComponent<BasicMovementController>();
		Animator = transform.FindChild("Sprite").GetComponent<Animator>();
		SpriteRenderer = transform.FindChild("Sprite").GetComponent<SpriteRenderer>();

		layerMasks.Ladders = LayerMask.GetMask(new string[] { "Ladders" });
		layerMasks.LadderTops = LayerMask.GetMask(new string[] { "LadderTops" });
		layerMasks.LadderSpines = LayerMask.GetMask(new string[] { "LadderSpines" });
		layerMasks.LadderBottoms = LayerMask.GetMask(new string[] { "LadderBottoms" });

		LadderGrabCheck = transform.Find("LadderGrabCheck").GetComponent<BoxCollider2D>();
		LadderDownGrabCheck = transform.Find("LadderDownGrabCheck").GetComponent<BoxCollider2D>();
		LadderBendCheck = transform.Find("LadderBendCheck").GetComponent<BoxCollider2D>();
		LadderFinishClimbingCheck = transform.Find("LadderFinishClimbingCheck").GetComponent<BoxCollider2D>();

		OperationState = StateName.Neutral;
		WalkSpeed = 1.25f;
		WalkSpeedMax = 1.25f;
		JumpSpeed = 5.0f;
	}
コード例 #9
0
    // Use this for initialization
    void Start()
    {
        moveController = gameObject.GetComponent <BasicMovementController>();
        animator       = gameObject.GetComponent <Animator>();

        playerBattleManager       = GameObject.Find("PlayerBattleManager");
        enemyBattleManager        = GameObject.Find("EnemyBattleManager");
        playerBattleManagerScript = playerBattleManager.GetComponent <PlayerBattleManager>();
        enemyBattleManagerScript  = enemyBattleManager.GetComponent <EnemyBattleManager>();
        myTargeting = gameObject.GetComponent <BasicTargeting>();
        myStats     = gameObject.GetComponent <UnitStats>();
        myStats.unitCurrentMoveSpeed = myStats.unitMoveSpeed;

        if (gameObject.tag == "PlayerUnit")
        {
            playerBattleManagerScript.mySpawnedUnits.Add(gameObject);
        }
        else
        {
            enemyBattleManagerScript.mySpawnedUnits.Add(gameObject);
        }
    }
コード例 #10
0
 void Awake()
 {
     statContinious     = GetComponent <StatContinious>();
     movementController = GetComponent <BasicMovementController>();
     dash = GetComponent <DashMove>();
 }
コード例 #11
0
    /// <summary>
    /// Every frame, we cast a number of rays below our character to check for platform collisions
    /// </summary>
    private void CastRaysBelow()
    {
        if (newPosition.y < -smallValue)
        {
            basicMovementState.isFalling = true;
        }
        else
        {
            basicMovementState.isFalling = false;
        }

        if ((currentParameters.gravity > 0) && (!basicMovementState.isFalling))
        {
            return;
        }

        float rayLength = rayBoundsRectangle.height / 2 + rayOffset;

        if (newPosition.y < 0)
        {
            rayLength += Mathf.Abs(newPosition.y);
        }


        Vector2 verticalRayCastFromLeft = new Vector2(rayBoundsRectangle.xMin + newPosition.x,
                                                      rayBoundsRectangle.center.y + rayOffset);
        Vector2 verticalRayCastToRight = new Vector2(rayBoundsRectangle.xMax + newPosition.x,
                                                     rayBoundsRectangle.center.y + rayOffset);

        RaycastHit2D[] hitsStorage           = new RaycastHit2D[numberOfVerticalRays];
        float          smallestDistance      = largeValue;
        int            smallestDistanceIndex = 0;
        bool           hitConnected          = false;

        for (int i = 0; i < numberOfVerticalRays; i++)
        {
            Vector2 rayOriginPoint = Vector2.Lerp(verticalRayCastFromLeft, verticalRayCastToRight, (float)i / (float)(numberOfVerticalRays - 1));

            if (((newPosition.y > 0) && (!basicMovementState.wasGroundedLastFrame)) || _speed.y > 0)
            {
                hitsStorage[i] = BasicMovementController.RayCast(rayOriginPoint, -Vector2.up, rayLength, platformMask & ~edgeColliderPlatformMask, true, Color.blue);
            }
            else
            {
                hitsStorage[i] = BasicMovementController.RayCast(rayOriginPoint, -Vector2.up, rayLength, platformMask, true, Color.blue);
            }

            if ((Mathf.Abs(hitsStorage[smallestDistanceIndex].point.y - verticalRayCastFromLeft.y)) < smallValue)
            {
                break;
            }

            if (hitsStorage[i])
            {
                hitConnected = true;
                if (hitsStorage[i].distance < smallestDistance)
                {
                    smallestDistanceIndex = i;
                    smallestDistance      = hitsStorage[i].distance;
                }
            }
        }
        if (hitConnected)
        {
            // if the character is jumping onto a (1-way) platform but not high enough, we do nothing
            if (!basicMovementState.wasGroundedLastFrame && smallestDistance < rayBoundsRectangle.size.y / 2)
            {
                basicMovementState.isCollidingBelow = false;
                return;
            }

            basicMovementState.isFalling        = false;
            basicMovementState.isCollidingBelow = true;

            newPosition.y = -Mathf.Abs(hitsStorage[smallestDistanceIndex].point.y - verticalRayCastFromLeft.y)
                            + rayBoundsRectangle.height / 2
                            + rayOffset;

            if (externalForce.y > 0)
            {
                newPosition.y += _speed.y * Time.deltaTime;
                basicMovementState.isCollidingBelow = false;
            }

            if (!basicMovementState.wasGroundedLastFrame && _speed.y > 0)
            {
                newPosition.y += _speed.y * Time.deltaTime;
            }



            if (Mathf.Abs(newPosition.y) < smallValue)
            {
                newPosition.y = 0;
            }

            // we check if the character is standing on a moving platform
            standingOn = hitsStorage[smallestDistanceIndex].collider.gameObject;
        }
        else
        {
            movingPlatformsCurrentGravity       = 0;
            basicMovementState.isCollidingBelow = false;
        }
    }
コード例 #12
0
    /// <summary>
    /// Casts rays to the sides of the character, from its center axis.
    /// If we hit a wall/slope, we check its angle and move or not according to it.
    /// </summary>
    private void CastRaysToTheSides(float dir)
    {
        /* float movementDirection=1;
         *      if ((_speed.x < 0) || (externalForce.x<0))
         *              movementDirection = -1; */
        float movementDirection = dir;

        float horizontalRayLength = Mathf.Abs(_speed.x * Time.deltaTime) + rayBoundsRectangle.width / 2 + rayOffset * 2;

        Vector2 horizontalRayCastFromBottom = new Vector2(rayBoundsRectangle.center.x,
                                                          rayBoundsRectangle.yMin + obstacleHeightTolerance);
        Vector2 horizontalRayCastToTop = new Vector2(rayBoundsRectangle.center.x,
                                                     rayBoundsRectangle.yMax - obstacleHeightTolerance);

        RaycastHit2D[] hitsStorage = new RaycastHit2D[numberOfHorizontalRays];

        for (int i = 0; i < numberOfHorizontalRays; i++)
        {
            Vector2 rayOriginPoint = Vector2.Lerp(horizontalRayCastFromBottom, horizontalRayCastToTop, (float)i / (float)(numberOfHorizontalRays - 1));

            if (basicMovementState.wasGroundedLastFrame && i == 0)
            {
                hitsStorage[i] = BasicMovementController.RayCast(rayOriginPoint, movementDirection * Vector2.right, horizontalRayLength, platformMask, true, Color.red);
            }
            else
            {
                hitsStorage[i] = BasicMovementController.RayCast(rayOriginPoint, movementDirection * Vector2.right, horizontalRayLength, platformMask & ~edgeColliderPlatformMask, true, Color.red);
            }

            if (hitsStorage[i].distance > 0)
            {
                float hitAngle = Mathf.Abs(Vector2.Angle(hitsStorage[i].normal, Vector2.up));

                if (hitAngle > currentParameters.maximumSlopeAngle)
                {
                    if (movementDirection < 0)
                    {
                        basicMovementState.isCollidingLeft = true;
                    }
                    else
                    {
                        basicMovementState.isCollidingRight = true;
                    }

                    basicMovementState.slopeAngleOK = false;

                    if (movementDirection <= 0)
                    {
                        newPosition.x = -Mathf.Abs(hitsStorage[i].point.x - horizontalRayCastFromBottom.x)
                                        + rayBoundsRectangle.width / 2
                                        + rayOffset * 2;
                    }
                    else
                    {
                        newPosition.x = Mathf.Abs(hitsStorage[i].point.x - horizontalRayCastFromBottom.x)
                                        - rayBoundsRectangle.width / 2
                                        - rayOffset * 2;
                    }

                    _speed = new Vector2(0, _speed.y);
                    break;
                }
            }
        }
    }