Exemplo n.º 1
0
    private void Update()
    {
        foreach (Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began && touch.fingerId == 0)
            {
                _touchPoint1 = new Vector2(touch.position.x, touch.position.y);

                if (!_firstTouched)
                {
                    StartCoroutine(DoubleTapCooldown());
                }

                else
                {
                    doubleTapStart?.Invoke();
                }
            }

            if (touch.phase == TouchPhase.Ended && touch.fingerId == 0)
            {
                doubleTapEnd.Invoke();
            }


            if (touch.phase == TouchPhase.Moved && touch.fingerId == 0)
            {
                _touchPoint2  = touch.position;
                MoveDirection = _touchPoint2 - _touchPoint1;
                MoveDirection.Normalize();

                TouchDistance = Vector2.Distance(_touchPoint1, _touchPoint2) / 250;

                if (TouchDistance > 1)
                {
                    TouchDistance = 1;
                }
            }
        }
    }
Exemplo n.º 2
0
	protected override void ApplyMove(float acceleration) {
		// validate that the agent should be able to walk at this angle
		float walkAngle = 90 - Vector3.Angle(GetUpOrientation(), rb.velocity);
		if(walkAngle > MaxWalkAngle || StandAngle > MaxWalkAngle) { IsStableOnGround = false; }
		// only apply gravity if the agent can't walk.
		if(!IsStableOnGround) { rb.velocity += gravity.GetGravityPower() * gravity.dir * Time.deltaTime; }
		VerticalVelocity = Vector3.Dot(rb.velocity, gravity.dir); // remember vertical velocity (for jumping!)
		// prevent moving up hill if the hill is too steep (videogame logic!)
		if(tooSteep && IsMovingIntentionally) {
			Vector3 acrossSlope = Vector3.Cross(GroundNormal, -gravity.dir).normalized;
			Vector3 intoSlope = Vector3.Cross(acrossSlope, -gravity.dir).normalized;
			float upHillAmount = Vector3.Dot(MoveDirection, intoSlope);
			if(upHillAmount > 0) {
				MoveDirection -= intoSlope * upHillAmount;
				MoveDirection.Normalize ();
			}
		}
		base.ApplyMove(acceleration);
		if(!IsStableOnGround) {
			rb.velocity -= gravity.dir * Vector3.Dot(rb.velocity, gravity.dir); // subtract donward-force from move speed
			rb.velocity += gravity.dir * VerticalVelocity; // re-apply vertical velocity from gravity/jumping
		}
	}
Exemplo n.º 3
0
 void GetInput()
 {
     MoveDirection = Vector2.zero;
     if (Input.GetKey(KeyCode.I))
     {
         ChangePlayerHealth(-10);
     }
     if (Input.GetKey(KeyCode.O))
     {
         ChangePlayerHealth(10);
     }
     if (Input.GetKey(KeyCode.W))
     {
         MoveDirection += Vector2.up;
         exitIndex      = 0;
     }
     if (Input.GetKey(KeyCode.A))
     {
         MoveDirection += Vector2.left;
         exitIndex      = 3;
     }
     if (Input.GetKey(KeyCode.S))
     {
         MoveDirection += Vector2.down;
         exitIndex      = 2;
     }
     if (Input.GetKey(KeyCode.D))
     {
         MoveDirection += Vector2.right;
         exitIndex      = 1;
     }
     Block();
     if (Input.GetKeyDown(KeyCode.Space))
     {
     }
     MoveDirection.Normalize();
 }
Exemplo n.º 4
0
        public override void Update()
        {
            MoveDirection = Vector2.Zero;
            AimDirection  = Vector2.Zero;

            // Left stick: movement
            var padDirection = Input.GetLeftThumb(ControllerIndex);
            var isDeadZone   = padDirection.Length() < DeadZone;

            if (!isDeadZone)
            {
                MoveDirection = padDirection;
            }
            MoveDirection.Normalize();

            // Right stick: aim
            padDirection = Input.GetRightThumb(ControllerIndex);
            var aimSpeed = padDirection.Length();

            isDeadZone = aimSpeed < DeadZone;
            // Make sure aim starts at 0 when outside deadzone
            aimSpeed = (aimSpeed - DeadZone) / (1.0f - DeadZone);
            // Clamp aim speed
            if (aimSpeed > 1.0f)
            {
                aimSpeed = 1.0f;
            }
            // Curve aim speed
            aimSpeed = (float)Math.Pow(aimSpeed, 1.6);
            if (!isDeadZone)
            {
                AimDirection = padDirection;
                AimDirection.Normalize();
                AimDirection *= aimSpeed;
            }

            // Keyboard move
            if (KeysLeft.Any(key => Input.IsKeyDown(key)))
            {
                MoveDirection += -Vector2.UnitX;
            }
            if (KeysRight.Any(key => Input.IsKeyDown(key)))
            {
                MoveDirection += +Vector2.UnitX;
            }
            if (KeysUp.Any(key => Input.IsKeyDown(key)))
            {
                MoveDirection += +Vector2.UnitY;
            }
            if (KeysDown.Any(key => Input.IsKeyDown(key)))
            {
                MoveDirection += -Vector2.UnitY;
            }

            var isAiming      = KeysAim.Any(key => Input.IsKeyDown(key)) || Input.GetLeftTrigger(ControllerIndex) >= DeadZone;
            var isFiring      = KeysShoot.Any(key => Input.IsKeyDown(key)) || Input.GetRightTrigger(ControllerIndex) >= DeadZone;
            var isStarting    = KeysStart.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.Start);
            var isReloading   = KeysReload.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.Y);
            var isInteracting = KeysInteract.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.A);

            if (isStarting)
            {
                OnStart?.Invoke(Entity);
            }

            if (isReloading)
            {
                OnReload?.Invoke(Entity);
            }

            if (isInteracting)
            {
                OnInteract?.Invoke(Entity);
            }

            // Mouse aim (after normalization of aim direction)
            // mouse aim is only enabled after you click the screen to lock your cursor, pressing escape cancels this
            if (Input.IsMouseButtonDown(MouseButton.Left))
            {
                Input.LockMousePosition(true);
            }
            if (Input.IsKeyPressed(Keys.Escape))
            {
                Input.UnlockMousePosition();
            }
            if (Input.IsMousePositionLocked)
            {
                // Mouse shooting
                if (Input.IsMouseButtonDown(MouseButton.Left))
                {
                    isFiring = true;
                }

                // Mouse aiming
                if (Input.IsMouseButtonDown(MouseButton.Right))
                {
                    isAiming = true;
                }

                AimDirection += new Vector2(Input.MouseDelta.X, -Input.MouseDelta.Y) * MouseSensitivity;
            }

            if (InvertXAxis)
            {
                AimDirection = new Vector2(-AimDirection.X, AimDirection.Y);
            }
            if (InvertYAxis)
            {
                AimDirection = new Vector2(AimDirection.X, -AimDirection.Y);
            }

            AimState  = isAiming;
            FireState = isFiring;
        }
        /// <summary>
        /// Character movement and physics updates. This includes both jumping and ground pound updates.
        /// </summary>
        protected void FixedUpdate()
        {
            // 1.) Handle jump requests
            if (Jump)
            {
                if (!isJumping)
                {
                    // 1.1) Jump if grounded
                    if (isGrounded)
                    {
                        if (animator)
                        {
                            animator.Jump = true;
                        }

                        isJumping  = true;
                        jumpTimer  = maxJumpTime;
                        isGrounded = false;

                        Vector3 jumpVelocity = body.velocity;
                        jumpVelocity += -gravity.GravityDirection * jumpSpeed;
                        body.velocity = jumpVelocity;

                        gravity.CanApplyGravity           = false;
                        gravity.CanChangeGravityDirection = false;
                    }
                    // 1.2) If already jumping and not using ground pound, use ground pound.
                    else if (!isUsingGroundPound && airTimeTimer > minAirTimeForPound)
                    {
                        if (animator)
                        {
                            animator.GroundPound = true;
                        }

                        Vector3 poundVelocity = body.velocity;
                        poundVelocity += gravity.GravityDirection * groundPoundSpeed;
                        body.velocity  = poundVelocity;
                        gravity.CanChangeGravityDirection = !overrideGravity;
                        isUsingGroundPound = true;
                        Jump = false;
                    }
                }

                if (isJumping)
                {
                    jumpTimer -= Time.deltaTime;

                    if (jumpTimer <= 0)
                    {
                        Jump = false;
                    }
                }
            }
            else
            {
                gravity.CanApplyGravity           = true;
                gravity.CanChangeGravityDirection = !overrideGravity;
                isJumping = false;
            }

            // 2.) Compute movement physics update based on input.
            Vector3 moveForce = Vector3.zero;

            if (MoveDirection.magnitude > 0)
            {
                MoveDirection.Normalize();
                lookDirection = MoveDirection;

                //MoveDirection = transform.rotation * MoveDirection;
                //moveForce = MoveDirection * groundedMoveForce;
                moveForce = transform.rotation * MoveDirection * groundedMoveForce;
            }

            if (!isGrounded)
            {
                moveForce *= airMoveForce;
            }
            else if (animator)
            {
                animator.Jump        = false;
                animator.GroundPound = false;
            }

            if (groundPoundRecoveryTimer <= 0.0f)
            {
                body.AddForce(moveForce, ForceMode.VelocityChange);
            }

            // 3.) Limit speed to maximum value. Gravity will need to be removed before computing speed and reintroduced in the final speed value to ensure an acceptable drop speed.
            body.drag = isGrounded && MoveDirection.magnitude > 0.1f ? groundDrag : airDrag;
            Vector3 movementVelocity = Vector3.ProjectOnPlane(body.velocity, -gravity.GravityDirection);
            Vector3 dropVelocity     = transform.InverseTransformDirection(body.velocity - movementVelocity);

            speed = movementVelocity.magnitude;

            if (dropVelocity.y <= 0.0f)
            {
                gravity.CanChangeGravityDirection = !overrideGravity;
            }

            if (speed > maxSpeed)
            {
                body.velocity = body.velocity - movementVelocity + movementVelocity.normalized * maxSpeed;
                speed         = maxSpeed;
            }

            if (animator)
            {
                animator.IsGrounded = IsGrounded;
                animator.Speed      = Speed;
            }
        }