Exemplo n.º 1
0
				private Animator anim;							// Reference to the Animator component.


				void Awake ()
				{
						// Setting up the references.
						anim = transform.root.gameObject.GetComponent<Animator> ();
						playerCtrl = transform.root.GetComponent<PlatformerCharacter2D> ();

						if (!playerCtrl.m_canShoot)
								Destroy (this.gameObject);
				}
        private void Awake()
        {
            character = GetComponent <PlatformerCharacter2D>();

            int enemyLayer  = LayerMask.NameToLayer("Enemy");
            int playerLayer = LayerMask.NameToLayer("Player");

            Physics2D.IgnoreLayerCollision(enemyLayer, playerLayer, false);
        }
				private Animator anim;						// Reference to the Animator on the player


				void Awake ()
				{
						// Setting up references.
						playerControl = GetComponent<PlatformerCharacter2D> ();
						healthBar = GameObject.Find ("HealthBar").GetComponent<SpriteRenderer> ();
						anim = GetComponent<Animator> ();

						// Getting the intial scale of the healthbar (whilst the player has full health).
						healthScale = healthBar.transform.localScale;
				}
Exemplo n.º 4
0
        private static Vector2 GetInitialPushForce(
            [NotNull] Collider2D metalSource,
            Vector2 playerPosition,
            float force,
            float pulseMultiplier,
            Vector2 velocity)
        {
            var box      = metalSource.bounds;
            var forceDir = PlatformerCharacter2D.GetForceDir(playerPosition, box).normalized;

            velocity = velocity.normalized;

            if (Vector2.Dot(forceDir, velocity) < 0f)
            {
                force *= 2;
            }
            return(forceDir * force * pulseMultiplier);
        }
Exemplo n.º 5
0
 private void Awake()
 {
     character = GetComponent <PlatformerCharacter2D>();
 }
 private void Awake()
 {
     character = GetComponent<PlatformerCharacter2D>();
 }
Exemplo n.º 7
0
 // Use this for initialization
 void Start()
 {
     playerController = FindObjectOfType <UnitySampleAssets._2D.PlatformerCharacter2D> ();
 }
Exemplo n.º 8
0
        /*
         * Public interface
         */

        public void Move(
            float horizontal,
            float vertical,
            bool crouch,
            bool jump,
            bool jumpHold,
            bool push,
            bool pushHold,
            bool pull)
        {
            if (this._isWin)
            {
                return;
            }
            var debug  = new StringWriter();
            var forces = new List <Vector2>();

            this._closestMetalSource =
                PlatformerCharacter2D.DetectMetal(
                    this.transform.position,
                    this._magnetRange,
                    this._whatIsMetal);

            var targetVelocity = this._maxSpeed * horizontal;
            var sign           = this._facingRight ? 1 : -1;

            Debug.DrawRay(this.transform.position, Vector2.left * this._magnetRange, Color.red);
            Debug.DrawRay(this.transform.position, Vector2.right * this._magnetRange, Color.red);
            Debug.DrawRay(this.transform.position, Vector2.up * this._magnetRange, Color.red);
            Debug.DrawRay(this.transform.position, Vector2.down * this._magnetRange, Color.red);

            // If crouching, check to see if the character can stand up
            if (!crouch && this._anim.GetBool("Crouch"))
            {
                // If the character has a ceiling preventing them from standing up, keep them crouching
                if (Physics2D.OverlapCircle(this._ceilingCheck.position, PlatformerCharacter2D.CeilingRadius,
                                            this._whatIsGround))
                {
                    crouch = true;
                }
            }

            // Set whether or not the character is crouching in the animator
            this._anim.SetBool("Crouch", crouch);

            //only control the player if grounded or airControl is turned on
            if ((this._grounded || this._airControl) && this._magnetAction != MagnetAction.Pull)
            {
                // Reduce the speed if crouching by the crouchSpeed multiplier
                horizontal = (crouch ? horizontal * this._crouchSpeed : horizontal);

                var baseAcceleration = Vector2.right * (targetVelocity - this._rigidBody2D.velocity.x);
                var acceleration     = Math.Sign(baseAcceleration.x) == sign
                    ? this._negativeAcceleration
                    : pushHold
                        ? this._pushAcceleration
                        : this._positiveAcceleration;
                forces.Add(baseAcceleration * acceleration);

                // Move the character
                //this._rigidBody2D.velocity = new Vector2(move * this._maxSpeed, this._rigidBody2D.velocity.y);

                // If the input is moving the player right and the player is facing left...
                if (horizontal > 0 && !this._facingRight ||
                    horizontal < 0 && this._facingRight)

                // ... flip the player.
                {
                    // Switch the way the player is labelled as facing.
                    this._facingRight = !this._facingRight;

                    // Multiply the player's x local scale by -1.
                    var theScale = this.transform.localScale;
                    theScale.x *= -1;
                    this.transform.localScale = theScale;
                }
            }


            // If the player should jump...
            if (this._grounded && jump && this._anim.GetBool("Ground"))
            {
                // Add a vertical force to the player.
                this._grounded = false;
                this._anim.SetBool("Ground", false);
                forces.Add(Vector2.up * this._jumpForce);
                this._source.PlayOneShot(this.JumpSound, 2f);
            }

            if (!this._grounded && !jumpHold && !pushHold && this._rigidBody2D.velocity.y > 0)
            {
                forces.Add(Vector2.down * this._jumpReleaseDamping);
            }

            this._rigidBody2D.gravityScale = this._rigidBody2D.velocity.y <= 0f
                ? this._baseGravityScale * this._fallMultiplier
                : this._baseGravityScale;

            if (pushHold || pull)
            {
                if (this._magnetAction == MagnetAction.Nothing)
                {
                    this._magnetAction = pushHold
                        ? MagnetAction.Push
                        : MagnetAction.Pull;
                    this._activeMetal = this._closestMetalSource;
                    if (this._activeMetal != null)
                    {
                        this._activeMetal.gameObject.GetComponent <SpriteRenderer>().color =
                            this._magnetAction == MagnetAction.Push
                                ? Color.blue
                                : Color.red;
                    }

                    this._source.PlayOneShot(this.BuzzStartSound, 2f);
                    this._buzzSource.volume = 1f;
                }

                // if (this._isMetalAbove || this._isMetalInFront || this._isMetalBehind)
                // {
                //     this._beam.Erase();
                // }
                // else if (this._activeMetal == null)
                // {
                //     this._beam.Draw(this._magnetRange);
                // }
                // else
                // {
                //     this._beam.Draw(Vector2.Distance(this._beam.transform.position, this._activeMetal.transform.position));
                // }
            }
            else
            {
                if (this._magnetAction != MagnetAction.Nothing)
                {
                    this._source.PlayOneShot(this.BuzzEndSound, 2f);
                    this._buzzSource.volume = 0f;
                }

                if (this._activeMetal != null)
                {
                    this._activeMetal.gameObject.GetComponent <SpriteRenderer>().color = Color.white;
                }
                this._magnetAction = MagnetAction.Nothing;
                this._activeMetal  = null;
                //this._beam.Erase();
            }

            switch (this._magnetAction)
            {
            case MagnetAction.Pull when this._activeMetal != null:
                if (this._isMetalUnderfoot || this._isMetalAbove || this._isMetalInFront || this._isMetalBehind)
                {
                    this._rigidBody2D.velocity     = Vector2.zero;
                    this._rigidBody2D.gravityScale = 0f;
                    forces.Clear();
                }
                else
                {
                    forces.Add(
                        PlatformerCharacter2D.GetPullForce(
                            this._activeMetal.transform.position,
                            this.transform.position,
                            this._magnetForce));
                }
                break;

            case MagnetAction.Push when push:
                // Add a vertical force to the player.
                this._grounded = false;
                this._anim.SetBool("Ground", false);
                forces.Add(
                    PlatformerCharacter2D.GetInitialPushForce(
                        this._activeMetal,
                        this.transform.position,
                        this._magnetForce,
                        this._pulseMultiplier,
                        this._rigidBody2D.velocity));
                this._source.PlayOneShot(this.PushSound, 2f);
                break;
            }

            var armPos = this._arm.position;

            var armTarget =
                (this._activeMetal != null
                    ? (Vector2)(this._activeMetal.transform.position - armPos)
                    : this._closestMetalSource != null
                        ? (Vector2)(this._closestMetalSource.transform.position - armPos)
                        : math.abs(vertical) < float.Epsilon
                            ? Vector2.right * sign
                            : new Vector2(horizontal, vertical))
                .normalized;

            var armAngle = math.atan2(1f * sign, 0f) - math.atan2(armTarget.x, armTarget.y);

            this._arm.transform.rotation = Quaternion.Euler(Vector3.forward * math.degrees(armAngle));

            var force          = forces.Aggregate(Vector2.zero, (x, y) => x + y);
            var effectiveForce = force + (Vector2)Physics.gravity;

            debug.WriteLine($"ActiveMetal: {this._activeMetal?.GetInstanceID()}");
            debug.WriteLine($"Push: {push}");
            Debug.DrawRay(this.transform.position, effectiveForce, Color.green);

            this.transform.position = new Vector3(
                math.max(this.MinX, this.transform.position.x),
                this.transform.position.y,
                this.transform.position.z);

            // The Speed animator parameter is set to the absolute value of the horizontal input.
            this._anim.SetFloat("Speed", Mathf.Abs(this._rigidBody2D.velocity.x));
            this._rigidBody2D.AddForce(force);
            this._debug.text = debug.ToString();
        }
 private void Awake()
 {
     this._character = this.GetComponent <PlatformerCharacter2D>();
 }
 private void Awake()
 {
     anim = GetComponent<Animator>();
     character = GetComponent<PlatformerCharacter2D>();
 }