public void AimInput() { #if UNITY_STANDALONE_WIN joystickImage.enabled = false; bgImage.enabled = false; if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); // Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); crosshair.transform.position = point; crosshair.DetectTargets(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { gunController.Aim(point); } } }
void Update() { Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); Ray ray = playerCam.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight()); float rayDist; if (groundPlane.Raycast(ray, out rayDist)) { Vector3 point = ray.GetPoint(rayDist); controller.LookAt(point); crosshair.position = point; if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { gunController.Aim(point); } } if (Input.GetMouseButton(0)) { gunController.OnMouseHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnMouseRelease(); } }
void Update() { //Movement Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); moveInput = playerCamera.transform.TransformDirection(moveInput); moveInput.y = 0; Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); //Rotation Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gun.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTarget(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { gun.Aim(point); } } //Weapon if (Input.GetMouseButton(0)) { gun.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gun.OnTriggerReleased(); } if (Input.GetKeyDown(KeyCode.R)) { gun.Reload(); } //Camera if (Input.GetKey(KeyCode.Q)) { camCtrl.Rotate('Q'); } if (Input.GetKey(KeyCode.E)) { camCtrl.Rotate('E'); } //Bounds if (transform.position.y < -20) { TakeDamage(health); } }
// Update contains the Enemy AI Algorithm private void Update() { // Run artificial intelligence code only if the Player is still alive if (player != null) { // Aim at the Player if (shouldAim) { gunController.Aim(player.transform.position); } // Check for objects blocking its view of the Player Ray ray = new Ray(transform.position, new Vector3(player.transform.position.x, transform.position.y + halfHeight, player.transform.position.z) - new Vector3(transform.position.x, transform.position.y + halfHeight, transform.position.z)); RaycastHit hit; // Is there a direct line of sight to the Player? bool directLineofSight = !Physics.Raycast(ray, out hit, Vector3.Distance(player.transform.position, transform.position), collideMask); Debug.DrawRay(ray.origin, ray.direction * Vector3.Distance(player.transform.position, transform.position), directLineofSight ? Color.green : Color.red); // If the Enemy can "see" the Player if (directLineofSight) { switch (intelligence) { case 2: // Aim at the player gunController.Aim(Globals.PlayerNextPosition); break; default: break; } // Shoot at the Player gunController.Shoot(); } else { // If the Player is obscured from vision, move till it is in view (and therefore 'shootable') if (navAgent.isActiveAndEnabled && (player != null)) { navAgent.SetDestination(player.transform.position); } } } }
public void AimGun(Vector3 point) { var aimPoint = new Vector2(point.x, point.z); var positionPoint = new Vector2(transform.position.x, transform.position.z); if (useWeaponAim && (aimPoint - positionPoint).sqrMagnitude > Mathf.Pow(minAimRadius, 2)) { gunController.Aim(point); } }
private void Update() { // Movement input: Store the axis for movement and apply them with the desired speed. The input is normalized for a better direction input. Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * m_MoveSpeed; m_Controller.Move(moveVelocity); // Look input: A ray from the camera is casted into an invisible plane, the player will always be looking at that specific point which is determined by the mouse cursor's position. Ray cursorRay = m_ViewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * m_GunController.GetHeight); float rayDistance; if (groundPlane.Raycast(cursorRay, out rayDistance)) { Vector3 point = cursorRay.GetPoint(rayDistance); //Debug.DrawLine (cursorRay.origin, point, Color.red); m_Controller.LookAt(point); m_Crosshairs.transform.position = point; //m_Crosshairs.DetectRays (cursorRay); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).magnitude > m_WeaponThreshold) { //print ((new Vector2 (point.x, point.z) - new Vector2 (transform.position.x, transform.position.z)).magnitude); if (m_GunController != null) { m_GunController.Aim(); } } // Weapon input: Whenver the left mouse button is clicked and the player is able to. The gun will be triggered. if (Input.GetMouseButton(0)) { m_GunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { m_GunController.OnTriggerReleased(); } if (Input.GetKeyDown(KeyCode.R)) { m_GunController.Reload(); } if (transform.position.y < m_FallingDeathThreshold) { TakeDamage(m_Health); } } }
void Update() { // movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; _controller.Move(moveVelocity); // look input // passing a ray through the main camera so that it hits the ground at the mouse position Ray ray = _viewCamera.ScreenPointToRay(Input.mousePosition); // generating a plane by passing the normal to the plane and the in-position // the normal is perpendicular to the plane // and since the plane lies flat, it's perpendicular would be Vector3.Up Plane groundPlane = new Plane(Vector3.up, Vector3.up * _GunController.GunHeight); float rayDistance; //this will return the ray distance from the camera to the ground if (groundPlane.Raycast(ray, out rayDistance)) { //this would return the intersection point by adding the distance to the ray origin Vector3 point = ray.GetPoint(rayDistance); _controller.LookAt(point); crosshairs.transform.position = point; // the crosshair position crosshairs.DetectTargets(ray); // print((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) //using sqr mabnitude, since 1^2 = 1, we compare with 1 { _GunController.Aim(point); } } // weapon input, shoots on left mouse button down and space key if (Input.GetMouseButton(0) || Input.GetKey(KeyCode.Space)) { _GunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0) || Input.GetKeyUp(KeyCode.Space)) { _GunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { _GunController.Reload(); } if (transform.position.y < -10) { TakeDamage(health); } }
void Update() { //Movement Input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); //Look Input Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 pointOfIntersection = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, pointOfIntersection, Color.red); controller.LookAt(pointOfIntersection); crossHairs.transform.position = pointOfIntersection; crossHairs.DetectTarget(ray); if ((new Vector2(pointOfIntersection.x, pointOfIntersection.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > Mathf.Pow(1.9f, 2f)) { gunController.Aim(pointOfIntersection); } } //Weapon Input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } if (transform.position.y < -10) { TakeDamage(health); } if (Input.GetMouseButtonDown(1)) { if (numOfGrenades != 0) { Launch(); numOfGrenades--; } } }
// Update is called once per frame void Update() { crosshair.ActivateCrosshair(false); closestEnemy = null; allEnemies = GameObject.FindObjectsOfType <Enemy>(); distanceClosestToEnemy = Mathf.Infinity; Vector3 moveInput = new Vector3(SimpleInput.GetAxisRaw("Horizontal"), 0, SimpleInput.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; playerController.Move(moveVelocity); Debug.Log(thumb.transform.position); Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); Debug.DrawLine(ray.origin, point, Color.red); playerController.LookAt(point); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { gunController.Aim(point); } } foreach (Enemy currentEnemy in allEnemies) { float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude; if (distanceToEnemy < distanceClosestToEnemy) { distanceClosestToEnemy = distanceToEnemy; closestEnemy = currentEnemy; //Debug.Log(distanceClosestToEnemy); if (distanceClosestToEnemy < 25) { crosshair.ActivateCrosshair(true); crosshair.DetectTargets(ray); playerController.LookAt(closestEnemy.transform.position); crosshair.transform.position = closestEnemy.transform.position; gunController.Shoot(); } } } if (Input.GetKeyDown(KeyCode.Space)) { gunController.Shoot(); } }
void Update() { //*Movement Input //GetAxisRaw removes smoothing of movement Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = Vector3.Normalize(moveInput) * moveSpeed; pController.Move(moveVelocity); //Kill player if fall off if (transform.position.y < -10) { TakeDamage(health); } //*Look Input //Cast ray from camera to mouse position Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gController.GunHeight); float rayDistance; //Intersect ray with plane, returns true if intersected, outputs ray distance if (groundPlane.Raycast(ray, out rayDistance)) { //Get point along ray with rayDistance Vector3 pointOfIntersection = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, pointOfIntersection, Color.red); pController.LookAt(pointOfIntersection); crosshairs.transform.position = pointOfIntersection; crosshairs.DetectTargets(ray); if ((new Vector2(pointOfIntersection.x, pointOfIntersection.z) - new Vector2(transform.position.x, transform.position.y)).sqrMagnitude > 1) { gController.Aim(pointOfIntersection); } } //*Weapon Input if (Input.GetMouseButton(0)) { gController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gController.Reload(); } if (Input.GetKeyDown(KeyCode.Escape)) { SceneManager.LoadScene("Menu"); } }
protected override void FixedUpdate() { if (hasTarget) { base.FixedUpdate(); float sqrDistToPoint = (new Vector2(target.position.x, target.position.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude; if (sqrDistToPoint > sqrDistToMuzzle) { gunController.Aim(target.position); } } }
void Update() { if (GameUI.IsPaused) { return; } // Movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); // Look input Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTargets(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).magnitude > 1) { gunController.Aim(point); } } // Weapon input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } if (transform.position.y < -10) { TakeDamage(Health); } }
void Update() { // Movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); // Jump Input if (Input.GetKeyDown(KeyCode.Space) && onGround) { controller.Jump(jump, doJump); onGround = false; } // Look input Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.gunPosHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); // Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); crosshair.transform.position = point; crosshair.crosshairDetectTarget(ray, rayDistance); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { gunController.Aim(point); } } // Weapon input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } }
// Update is called once per frame void Update() { //movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); //look input Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane plane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (plane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); // Debug.DrawLine( ray.origin, point, Color.red ); controller.LookAt(point); crossHair.transform.position = point; crossHair.DetectTargets(ray); //square magnitude is faster to compute float sqrDistance = (new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude; //print( distance ); if (sqrDistance > Mathf.Pow(aimThreshold, 2)) { gunController.Aim(point); } } //weapon input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } else if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } if (transform.position.y < -10) { TakeDamage(health); } }
// Update is called once per frame void Update() { //movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); //look input Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * transform.position.y); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) //will return true if it intersepts with the ground plane { Vector3 point = ray.GetPoint(rayDistance); //returns the point that the ray has hit the plane //Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTargets(ray); //sqr magnitude faster than normal magnitude if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 2) { gunController.Aim(point); } } //weapon input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } if (transform.position.y < -10) { TakeDamage(health); } }
void Update() { //玩家移动Movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controllor.Move(moveVelocity); #region #########鼠标光标系统(look input) Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); //鼠标光标移动,有点像瞄准系统记录你在看的位置 Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); //射线ray投射到plane上 float rayDistance; //获得鼠标光标位置 if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, point,Color.red); controllor.lookAt(point); crosshairs.transform.position = point; crosshairs.DectectTarget(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).magnitude > 1) { gunController.Aim(point); } } ; #endregion //Weapon Input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } //玩家掉出地图外,死亡 if (transform.position.y < -10) { TakeDamage(health); } }
// Update function called once per frame private void Update() { // Get input from Player, then store it in a normalised vector (i.e. of UNIT distance) latestInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized; // Multiply the velocity by the movement speed velocity = latestInput * movementSpeed; // Rotate player to match direction of movement float targetAngle = Mathf.Atan2(latestInput.x, latestInput.z) * Mathf.Rad2Deg; angle = Mathf.LerpAngle(angle, targetAngle, Time.deltaTime * turnSpeed * latestInput.magnitude); // Credits to Sebastian Lague for his code on Mouse Ray Plane Intersection Ray mouseRay = mainCamera.ScreenPointToRay(Input.mousePosition); float distanceToIntersection; // Intersect mouse ray with a Plane to fix parallax issue Plane eyeLevelIntersectionPlane = new Plane(Vector3.up, Vector3.up * (gunController.Gun.transform.position.y)); if (eyeLevelIntersectionPlane.Raycast(mouseRay, out distanceToIntersection)) { Vector3 pt = mouseRay.GetPoint(distanceToIntersection); // Last line of Seb. Lague code // Set the crosshair position and colour, if it is not equal to null if (crosshair) { // Move crosshair crosshair.transform.position = pt; crosshair.GetComponent <CrosshairAnim>().MatchTargets(mouseRay); } // Aims the gun at the crosshair gunController.Aim(pt); } // Draw a debug ray from the camera outwards, in the direction of the mouse. This is only visible in the Scene view within Unity Debug.DrawRay(mouseRay.origin, mouseRay.direction * 1000, Color.red); // Update the Globals Information Datastore with the Player's updated position for the next frame Globals.PlayerPosition = gameObject.transform.position; Globals.PlayerNextPosition = (gameObject.transform.position + (velocity * Time.fixedDeltaTime)) + (velocity * Time.fixedDeltaTime); // Shoot the gun if (Input.GetMouseButton(0)) { gunController.Shoot(); } }
void Update() { // MOVEMENT INPUT Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; playerController.Move(moveVelocity); // questa funzione sarà nel controller // LOOK INPUT Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); // Debug.DrawLine(ray.origin, point, Color.red); playerController.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTargets(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 2) { gunController.Aim(point); } } // DIE IF FALLING if (transform.position.y < -10) // TODO: smooth damage { TakeDamage(health); } // WEAPON INPUT if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } }
void Update() { //이동을 입력받는 곳 Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); //바라보는 방향을 입력받는 곳 Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, point, Color.red); controller.LookAt(point); crosshair.transform.position = point; crosshair.DetectTarget(ray); if ((new Vector3(point.x, point.y, point.z) - new Vector3(transform.position.x, transform.position.y, transform.position.z)).sqrMagnitude > 2f) { gunController.Aim(point); } } //무기 발사를 입력받는 곳 if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } if (transform.position.y < -10) { TakeDamage(health); } }
private void LookInput() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Plane ground = new Plane(Vector3.up, Vector3.up * _gunController.GunHeight); if (ground.Raycast(ray, out float rayDistance)) { Vector3 pos = ray.GetPoint(rayDistance); _playerController.LookAt(pos); if ((new Vector2(pos.x, pos.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { _gunController.Aim(pos); } } }
// Update is called once per frame void Update() { // Movement Input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); // Look input Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); // we need to intersect the ray with the ground plane to see where the player will be looking - so make a flat plane float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) // if ray intersects with ground plane { Vector3 pointOfIntersection = ray.GetPoint(rayDistance); Debug.DrawLine(ray.origin, pointOfIntersection, Color.red); controller.LookAt(pointOfIntersection); crosshairs.transform.position = pointOfIntersection; crosshairs.DetectTargets(ray); if ((new Vector2(pointOfIntersection.x, pointOfIntersection.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude >= 1) { gunController.Aim(pointOfIntersection); } { } } // Weapon input if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } if (transform.position.y <= -10) { TakeDamage(health); } }
protected override void Update() { base.Update(); // Movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized; Vector3 moveVelocity = moveInput * moveSpeed; controller.Move(moveVelocity); // Look input //Plane groundPlane = new Plane(Vector3.up, Vector3.zero); Plane groundPlane = new Plane(Vector3.up, Vector3.up * _verticalOffset); Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); float distanceToGroundPlane; if (groundPlane.Raycast(ray, out distanceToGroundPlane)) { Vector3 pointOfIntersection = ray.GetPoint(distanceToGroundPlane); pointOfIntersection = new Vector3(pointOfIntersection.x, _verticalOffset, pointOfIntersection.z); controller.LookAt(pointOfIntersection); crosshairs.transform.position = pointOfIntersection; crosshairs.DetectTargets(ray); if (Vector2.Distance(new Vector2(pointOfIntersection.x, pointOfIntersection.z), new Vector2(transform.position.x, transform.position.z)) > enableAimingRadius) { gunCtrl.Aim(pointOfIntersection); } } // Weapon input if (Input.GetMouseButtonUp(0)) { gunCtrl.OnTriggerRelease(); } if (Input.GetMouseButtonDown(0)) { gunCtrl.OnTriggerHold(); } if (Input.GetMouseButtonDown(1)) { gunCtrl.Reload(); } CameraFollowPlayer(); }
private void LookInput() { Ray ray = _viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * _gunController.GunHeight); if (groundPlane.Raycast(ray, out float rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, point, Color.red); _controller.LookAt(point); Crosshairs.transform.position = point; Crosshairs.DetectTargets(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1) { _gunController.Aim(point); } } }
void Update() { Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * playerSpeed; _playerController.Move(moveVelocity); //срздание луча из камеры до мышки Ray ray = _mainCamera.ScreenPointToRay(Input.mousePosition); //создать плейн и определить направление нормалей у него - Vector3.up Plane groundPlane = new Plane(Vector3.up, Vector3.up * _gunController.GunHeight); float rayDistance; //утверждение будет верным если луч соприкоснётся с нашим новым плейном if (groundPlane.Raycast(ray, out rayDistance)) { //через метод луча узнаем о точке пересечения луча и поля Vector3 pointOfIntersection = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, pointOfIntersection,Color.red); _playerController.LookAt(pointOfIntersection); crosshairs.transform.position = pointOfIntersection; crosshairs.DetectTargets(ray); if ((new Vector2(pointOfIntersection.x, pointOfIntersection.z) - new Vector2(transform.position.x, transform.position.y)).sqrMagnitude > 20f) { _gunController.Aim(pointOfIntersection); } } if (Input.GetMouseButton(0)) { _gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { _gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { _gunController.Reloading(); } }
private void Update() { // Movement input Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; mController.Move(moveVelocity); // Look input Ray ray = mMainCam.ScreenPointToRay(Input.mousePosition); Plane virtualGround = new Plane(Vector3.up, Vector3.up * mGunController.GunHeight); float rayDistance; if (virtualGround.Raycast(ray, out rayDistance)) { Vector3 endPoint = ray.GetPoint(rayDistance); // Debug.DrawLine(ray.origin, endPoint, Color.black); mController.LookAt(endPoint); crosshairs.transform.position = endPoint; crosshairs.DetectTargets(ray); if ((new Vector2(endPoint.x, endPoint.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 2 * 2) { mGunController.Aim(endPoint); } } // Weapon input if (Input.GetMouseButton(0)) { mGunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { mGunController.OnTriggerRelease(); } //out of world edge if (transform.position.y < -5f) { TakeHitWithDamage(health); } }
/// <summary> /// Handles the player rotation during the update phase. This represents where we want the player to look. /// </summary> private void HandlePlayerRotation() { // show a ray from the camera down to the mouse position on the ground Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); // we don't need a reference to the ground in the scene; we instead can generate one programmatically and that can serve as the ground Plane groundPlane = new Plane( Vector3.up, // pass in the normal of the plane, which is a direction perpendicular to a plane that is lying flat Vector3.up * gunController.GunHeight // the endpoint doesn't matter ); // if the ray intersects (i.e. hit) the ground plane, then we'll know the length from camera to intersection (i.e. ray distance) float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { // actual point of intersection Vector3 point = ray.GetPoint(rayDistance); // draw a line in the scene view showing where the player will be looking if (debug) { Debug.DrawLine(ray.origin, point, Color.red); } // rotate the player to look at the point playerController.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTarget(ray); // debug for finding distance between crosshair dot point and player (to determine when crosshairs are too close) // squared magnitude is faster than magnitude float distance = (new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude; if (distance > 1.21f) { // better aiming to crosshairs gunController.Aim(point); } } }
private IEnumerator IEAimAction() { while (!_player.IsDeath) { yield return(null); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Plane plane = new Plane(Vector3.up, Vector3.up * _controller.GunHeight); if (plane.Raycast(ray, out float rayDistance)) { Vector3 pos = ray.GetPoint(rayDistance); LookAt(pos); if ((new Vector2(pos.x, pos.z) - new Vector2(_player.transform.position.x, _player.transform.position.z)).sqrMagnitude > 1) { _controller.Aim(pos); } } } }
void Update() { //移动 Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelpcity = moveInput.normalized * moveSpeed; playerController.Move(moveVelpcity); //转向 Ray ray = camera.ScreenPointToRay(Input.mousePosition); Plane plane = new Plane(Vector3.up, Vector3.up * gunController.gunHeight); float rayDistance; if (plane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin, point, Color.red); playerController.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTargets(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 0.5f) { gunController.Aim(point); } } //射击 if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } }
void Update() { Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.Move(moveVelocity); Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); controller.LookAt(point); crossHairs.transform.position = point; crossHairs.DetectTargets(ray); if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1f) { gunController.Aim(point); } } if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } }
void Update() { //Movement Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; controller.move(moveVelocity); //Look Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight()); float rayDistance; if (groundPlane.Raycast(ray, out rayDistance)) { Vector3 point = ray.GetPoint(rayDistance); //Debug.DrawLine(ray.origin,point,Color.red); controller.LookAt(point); crosshairs.transform.position = point; crosshairs.DetectTargets(ray); gunController.Aim(point); } //Weapon if (Input.GetMouseButton(0)) { gunController.OnTriggerHold(); } if (Input.GetMouseButtonUp(0)) { gunController.OnTriggerRelease(); } if (Input.GetKeyDown(KeyCode.R)) { gunController.Reload(); } }