// Aim will determine which direction the player in inputting to aim in public string Aim() { keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Keys.Up)) { aimDirection = AimDirection.Up; return("up"); } if (keyState.IsKeyDown(Keys.Right)) { aimDirection = AimDirection.Forward; return("right"); } if (keyState.IsKeyDown(Keys.Left)) { aimDirection = AimDirection.Forward; return("left"); } if (keyState.IsKeyDown(Keys.Up) && keyState.IsKeyDown(Keys.Right)) { aimDirection = AimDirection.Diagonal; return("diagonal right"); } if (keyState.IsKeyDown(Keys.Up) && keyState.IsKeyDown(Keys.Left)) { aimDirection = AimDirection.Diagonal; return("diagonal left"); } else { return(null); } }
// note this currently only calculates with mouse input. For Controller support, will need // to process Aim Direction differently private Vector2 CalculateAimDirection() { AimDirection = _camera.ScreenToWorldPoint(_input.MousePosition) - transform.position; AimDirection.Normalize(); Debug.Log("AimDirection: " + AimDirection); return(AimDirection); }
public void Start() { AimDirection direction = physics.GetVelocityRef().ClosestDirection(); // going left? bool flipX = direction == AimDirection.LEFT || direction == AimDirection.UP_LEFT || direction == AimDirection.DOWN_LEFT; switch (direction) { case AimDirection.UP: animator.SetBool(Up, true); break; case AimDirection.DOWN: animator.SetBool(Down, true); break; case AimDirection.UP_LEFT: case AimDirection.UP_RIGHT: animator.SetBool(DiagUp, true); break; case AimDirection.DOWN_LEFT: case AimDirection.DOWN_RIGHT: animator.SetBool(DiagDown, true); break; default: animator.SetBool(Horizontal, true); break; } spriteRenderer.flipX = flipX; }
public void Update() { float aimX = Input.GetAxis("Aim X"); float aimY = Input.GetAxis("Aim Y"); bool hasGamepad = Input.GetJoystickNames().Length != 0 && Input.GetJoystickNames().Any(x => x.Length != 0); if (Controls.UseMouseToAim && !hasGamepad) { Vector3 mouseInWorld = camera.ScreenToWorldPoint(Input.mousePosition); aimX = mouseInWorld.x - transform.position.x; aimY = mouseInWorld.y - transform.position.y; } aimVector.x = aimX; aimVector.y = aimY; AimDirection aimDirection = aimVector.ClosestDirection(); if (aimDirection != AimDirection.LAST) { direction = aimDirection; } }
/** * Get the angle corresponding to this aim direction * Returns NaN for AimDirection.LAST */ public static float Angle(this AimDirection direction) { if (direction == AimDirection.LAST) { return(float.NaN); } return((int)direction * anglePerSection); }
private void Update() { AimDirection aim = physics.GetVelocityRef().ClosestDirection(); if (aim != AimDirection.LAST) { direction = aim; } }
// constructor public Player(Rectangle hBox, Texture2D texture) : base(hBox, texture) { keyState = new KeyboardState(); aimDirection = AimDirection.Forward; speedX = 5; speedY = 0; // set rectangles for collsion detection leftSide = new Rectangle(hitBox.X, hitBox.Y, -3, hitBox.Height); rightSide = new Rectangle((hitBox.X + hitBox.Width), hitBox.Y, 3, hitBox.Height); topSide = new Rectangle(hitBox.X, hitBox.Y, hitBox.Width, -3); bottomSide = new Rectangle(hitBox.X, (hitBox.Y + hitBox.Height), hitBox.Width, 3); }
public float GetAngleForDirection(AimDirection checkDir) { switch (checkDir) { case AimDirection.eAimDirectionN: { return(0f); } case AimDirection.eAimDirectionNE: { return(-45f); } case AimDirection.eAimDirectionNW: { return(45); } case AimDirection.eAimDirectionS: { return(180f); } case AimDirection.eAimDirectionSE: { return(-135f); } case AimDirection.eAimDirectionSW: { return(135f); } case AimDirection.eAimDirectionE: { return(-90f); } case AimDirection.eAimDirectionW: { return(90f); } default: return(0f); } }
public void Update() { if (cooldown > 0) { cooldown -= Time.deltaTime; } if (ShouldShoot() && CanShoot()) { AimDirection direction = aim.GetDirection(); float angle = direction.Angle(); float cosangle = Mathf.Cos(angle); float sinangle = Mathf.Sin(angle); SpawnBullet(angle, cosangle * speed, sinangle * speed); cooldown = shootCooldown; } }
// Update is called once per frame void Update () { if( !rootObj ) { rootObj = transform.root.gameObject; return; } switch(mSpriteSelType) { case SelectionType.eSelectionAim: { PlayerAimController playerAimControl = rootObj.GetComponent<PlayerAimController>(); if( playerAimControl) { currentDirection = playerAimControl.GetAimDirection(); } break; } case SelectionType.eSelectionMove: { PlayerMoveController playerMove = rootObj.GetComponent<PlayerMoveController>(); if(playerMove) { currentDirection = playerMove.GetAimDirection(); } break; } } if(m_directionalSprites.ContainsKey(currentDirection)) { SpriteRenderer spriteRender = GetComponent<SpriteRenderer>(); if(spriteRender) { SpriteSelection chosen = m_directionalSprites[currentDirection]; spriteRender.sprite = chosen.spriteSelection; } } if(bVerboseLogging) { Debug.Log("Current direction :" + currentDirection.ToString()); } }
// Update is called once per frame void Update() { if (!rootObj) { rootObj = transform.root.gameObject; return; } switch (mSpriteSelType) { case SelectionType.eSelectionAim: { PlayerAimController playerAimControl = rootObj.GetComponent <PlayerAimController>(); if (playerAimControl) { currentDirection = playerAimControl.GetAimDirection(); } break; } case SelectionType.eSelectionMove: { PlayerMoveController playerMove = rootObj.GetComponent <PlayerMoveController>(); if (playerMove) { currentDirection = playerMove.GetAimDirection(); } break; } } if (m_directionalSprites.ContainsKey(currentDirection)) { SpriteRenderer spriteRender = GetComponent <SpriteRenderer>(); if (spriteRender) { SpriteSelection chosen = m_directionalSprites[currentDirection]; spriteRender.sprite = chosen.spriteSelection; } } if (bVerboseLogging) { Debug.Log("Current direction :" + currentDirection.ToString()); } }
public float GetAngleForDirection( AimDirection checkDir ) { switch( checkDir) { case AimDirection.eAimDirectionN: { return 0f; } case AimDirection.eAimDirectionNE: { return -45f; } case AimDirection.eAimDirectionNW: { return 45; } case AimDirection.eAimDirectionS: { return 180f; } case AimDirection.eAimDirectionSE: { return -135f; } case AimDirection.eAimDirectionSW: { return 135f; } case AimDirection.eAimDirectionE: { return -90f; } case AimDirection.eAimDirectionW: { return 90f; } default: return 0f; } }
void Update() { if (player == null) { player = playerFinder.getPlayerTransform(); AI.setbuttonHeldAimRight(true); AI.setbuttonHeldAimLeft(false); AI.setbuttonHeldAimUp(false); AI.setbuttonHeldAimDown(false); return; } if (player.position.x > transform.position.x && AISync.getFacingRight() == false) { aimLeft = false; aimRight = true; AI.setbuttonHeldAimLeft(aimLeft); AI.setbuttonHeldAimRight(aimRight); AI.setbuttonHeldAimUp(aimUp); AI.setbuttonHeldAimDown(aimDown); return; } else if (player.position.x < transform.position.x && AISync.getFacingRight() == true) { aimRight = false; aimLeft = true; AI.setbuttonHeldAimRight(aimRight); AI.setbuttonHeldAimLeft(aimLeft); AI.setbuttonHeldAimUp(aimUp); AI.setbuttonHeldAimDown(aimDown); return; } Vector3 playerPos = player.transform.position - new Vector3(0, 5, 0); Vector2 origin = new Vector2(0f, 0f); //Init Vector2 hitDirection = new Vector2(0f, 0f); //Init AimDirection vectorDirection = 0; Quaternion rotation; Vector2 direction; if (AISync.getFacingRight() == true) { if (player.position.y >= transform.position.y) { //Up aimUp = true; aimDown = false; //Up rotation = spawn4.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.right; vector1 = direction * 50; vector2 = (playerPos - spawn4.position); angle1 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn4.position, vector1, Color.yellow); Debug.DrawRay(spawn4.position, vector2, Color.white); angleEnum1 = AimDirection.Up; Up = vector1; //Up Right rotation = spawn3.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.right; vector1 = direction * 50; vector2 = (playerPos - spawn3.position); angle2 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn3.position, vector1, Color.yellow); Debug.DrawRay(spawn3.position, vector2, Color.white); angleEnum2 = AimDirection.UpRight; UpRight = vector1; } else { //Down aimUp = false; aimDown = true; //Down rotation = spawn0.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.right; Vector3 spawn0Pos = spawn0.position - v0_4_0; vector1 = direction * 50; vector2 = (playerPos - spawn0Pos); angle1 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn0Pos, vector1, Color.yellow); Debug.DrawRay(spawn0Pos, vector2, Color.white); angleEnum1 = AimDirection.Down; Down = vector1; //Down Right rotation = spawn1.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.right; vector1 = direction * 50; vector2 = (playerPos - spawn1.position); angle2 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn1.position, vector1, Color.yellow); Debug.DrawRay(spawn1.position, vector2, Color.white); angleEnum2 = AimDirection.DownRight; DownRight = vector1; } if (angle1 > angle2) { angle1 = angle2; vectorDirection = angleEnum2; aimRight = true; } else { aimRight = false; aimLeft = false; vectorDirection = angleEnum1; } //Right vector1 = new Vector3(spawn2.position.x + 50, spawn2.position.y, 0) - spawn2.position; vector2 = (playerPos - spawn2.position); angle3 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn2.position, new Vector3(50, 0, 0), Color.yellow); Debug.DrawRay(spawn2.position, vector2, Color.white); Right = vector1; if (angle1 > angle3) { angle1 = angle3; vectorDirection = AimDirection.Right; aimUp = false; aimDown = false; } // If the AI gets close to a target it'll aim up if (Mathf.Abs(transform.position.x - player.position.x) < 10f && Mathf.Abs(transform.position.y - player.position.y) < 3) { angle1 = angle3; vectorDirection = AimDirection.Right; aimUp = false; aimDown = false; } AI.setbuttonHeldAimRight(aimRight); AI.setbuttonHeldAimLeft(aimLeft); AI.setbuttonHeldAimUp(aimUp); AI.setbuttonHeldAimDown(aimDown); } else if (AISync.getFacingRight() == false) { if (player.position.y >= transform.position.y) { //Up aimUp = true; aimDown = false; //Up rotation = spawn4.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.left; vector1 = direction * 50; vector2 = (playerPos - spawn4.position); angle1 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn4.position, vector1, Color.yellow); Debug.DrawRay(spawn4.position, vector2, Color.white); angleEnum1 = AimDirection.Up; Up = vector1; //Up Left rotation = spawn3.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.left; vector1 = direction * 50; vector2 = (playerPos - spawn3.position); angle2 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn3.position, vector1, Color.yellow); Debug.DrawRay(spawn3.position, vector2, Color.white); angleEnum2 = AimDirection.UpLeft; UpLeft = vector1; } else { //Down aimUp = false; aimDown = true; //Down Vector3 spawn0Pos = spawn0.position - v0_4_0; rotation = spawn0.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.left; vector1 = direction * 50; vector2 = (playerPos - spawn0Pos); angle1 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn0Pos, vector1, Color.yellow); Debug.DrawRay(spawn0Pos, vector2, Color.white); angleEnum1 = AimDirection.Down; Down = vector1; //Down Left rotation = spawn1.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.left; vector1 = direction * 50; vector2 = (playerPos - spawn1.position); angle2 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn1.position, vector1, Color.yellow); Debug.DrawRay(spawn1.position, vector2, Color.white); angleEnum2 = AimDirection.DownLeft; DownLeft = vector1; } if (angle1 > angle2) { angle1 = angle2; vectorDirection = angleEnum2; aimLeft = true; } else { aimRight = false; aimLeft = false; vectorDirection = angleEnum1; } //Left rotation = spawn2.rotation; rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z * -1); direction = rotation * Vector2.left; vector1 = direction * 50; vector2 = (playerPos - spawn2.position); angle3 = Vector3.Angle(vector1, vector2); Debug.DrawRay(spawn2.position, vector1, Color.yellow); Debug.DrawRay(spawn2.position, vector2, Color.white); Left = vector1; if (angle1 > angle3) { angle1 = angle3; vectorDirection = AimDirection.Left; aimUp = false; aimDown = false; } // If the AI gets close to a target it'll aim up if (Mathf.Abs(transform.position.x - player.position.x) < 5f && Mathf.Abs(transform.position.y - player.position.y) < 3) { angle1 = angle3; vectorDirection = AimDirection.Left; aimUp = false; aimDown = false; } AI.setbuttonHeldAimRight(aimRight); AI.setbuttonHeldAimLeft(aimLeft); AI.setbuttonHeldAimUp(aimUp); AI.setbuttonHeldAimDown(aimDown); } if (vectorDirection == AimDirection.Up) { origin = spawn4.position; hitDirection = Up; } else if (vectorDirection == AimDirection.Down) { origin = spawn0.position - v0_4_0; hitDirection = Down; } else if (vectorDirection == AimDirection.UpRight) { origin = spawn3.position; hitDirection = UpRight; } else if (vectorDirection == AimDirection.UpLeft) { origin = spawn3.position; hitDirection = UpLeft; } else if (vectorDirection == AimDirection.DownRight) { origin = spawn1.position; hitDirection = DownRight; } else if (vectorDirection == AimDirection.DownLeft) { origin = spawn1.position; hitDirection = DownLeft; } else if (vectorDirection == AimDirection.Right) { origin = spawn2.position; hitDirection = Right; } else if (vectorDirection == AimDirection.Left) { origin = spawn2.position; hitDirection = Left; } Hit = Physics2D.Raycast(origin, hitDirection, 100f); if (Hit.transform == null || (Hit.transform != null && String.Compare(Hit.transform.name, 0, "Cube", 0, 4) == 0)) { aimUp = false; aimDown = false; AI.setbuttonHeldAimUp(aimUp); AI.setbuttonHeldAimDown(aimDown); } }
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; }
// Update is called once per frame when our player object tells us to public void UpdateMovement () { if( !HotInputManager.sInstance ) { return; } Vector3 newMov = HotInputManager.sInstance.GetMoveVector(mPlayerId); moveVector = Vector3.zero; moveHorizontal = newMov.x; moveVertical = newMov.y; bool bMoving = (moveHorizontal != 0f || moveVertical != 0f); if(momentumPercent > 0f || bMoving) { momentumPercent += (bMoving) ? (momentumSpeedUpRate * Time.deltaTime) : -(momentumSlowDownRate * Time.deltaTime); } momentumPercent = Mathf.Clamp01(momentumPercent); currentSpeed = momentumPercent * maxSpeed; moveVector.x = moveHorizontal; moveVector.y = moveVertical; moveVector = Vector3.Normalize(moveVector); float gameRadius = 50f; if( GameBounds.sBounds ) { gameRadius = GameBounds.sBounds.GetGameRadius(); Vector3 localPosition = transform.position; if( Vector3.Distance(Vector3.zero, localPosition + moveVector ) > gameRadius) { // Unless we're heading back in, we don't want to let the player go this way moveVector = Vector3.zero - localPosition; } } if( currentSpeed == 0f) { m_currentDirection = m_defaultDirection; if(m_movementAnim) { m_movementAnim.SetBool("IsMoving", false); } } else { m_currentDirection = GetAimDirection(); if (m_movementAnim) { m_movementAnim.SetBool("IsMoving", true); } } transform.position = Vector3.MoveTowards(transform.position, transform.position + moveVector, Time.deltaTime * currentSpeed); }