예제 #1
0
    void Update()
    {
        //Movement input
        Vector3 moveInput    = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        Vector3 moveVelociry = moveInput.normalized * moveSpeed;

        controller.Move(moveVelociry);

        //Look input
        Ray   ray         = viewCamera.ScreenPointToRay(Input.mousePosition);
        Plane groundPlane = new Plane(Vector3.up, Vector3.up * weaponController.WeaponHeight);
        float rayDistance;

        if (groundPlane.Raycast(ray, out rayDistance))
        {
            Vector3 point = ray.GetPoint(rayDistance);
            controller.LookAt(point);
            crosshair.transform.position = point;
            crosshair.DetectTargets(ray);
        }

        //Weapon input
        if (Input.GetMouseButton(0))
        {
            weaponController.OnTriggerHold();
        }
        if (Input.GetMouseButtonUp(0))
        {
            weaponController.OnTriggerRelease();
        }
    }
    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    = defCam.ScreenPointToRay(Input.mousePosition);
        Plane ground = new Plane(Vector3.up, Vector3.up * gunController.GunHeight);

        if (ground.Raycast(ray, out float rayDist))
        {
            Vector3 pointOfIntersect = ray.GetPoint(rayDist);
            Debug.DrawLine(defCam.transform.position, pointOfIntersect, Color.green);
            controller.LookAt(pointOfIntersect);
            crosshair.transform.position = pointOfIntersect;
            crosshair.DetectTargets(ray);
        }

        //GUN INPUT
        if (Input.GetMouseButton(0))
        {
            gunController.OnTriggerHold();
        }
        if (Input.GetMouseButtonUp(0))
        {
            gunController.OnTriggerRelease();
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            gunController.Reload();
        }
    }
예제 #3
0
    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);
            }
        }
    }
예제 #4
0
    void Update()
    {
        //movement input
        checkHorizontal();
        checkVertical();
        float x = Input.GetAxisRaw("Horizontal"); //get key input for x
        float y = Input.GetAxisRaw("Vertical");   //get key input for y

        controller.MoveX(x);
        controller.MoveY(y);
        controller.Dashing(dashing);

        //Look input
        Ray   ray         = viewCamera.ScreenPointToRay(Input.mousePosition); //cast a ray from camera to mouse cursor
        Plane groundPlane = new Plane(Vector3.up, Vector3.up * weaponController.WeaponHeight);
        float rayDistance;                                                    //distance from ray to cursor

        if (groundPlane.Raycast(ray, out rayDistance))                        //pass in the ray and get the rayDistance
        {
            Vector3 point = ray.GetPoint(rayDistance);                        //get rayDistance and save to point
            Debug.DrawLine(ray.origin, point, Color.red);
            controller.LookAt(point);                                         //run PlayerController's "LookAt" function
            crosshair.transform.position = point;                             //set position of cursor to point
            crosshair.DetectTargets(ray);                                     //run Crosshair's "DetectTarget" function

            //if cursor position is not close to player(to prevent wierd behaviour when weapon aims to near at player)
            if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1)
            {
                weaponController.Aim(point); //aim weapon at the cursor point
            }
        }

        //weapon input
        if (Input.GetMouseButton(0))
        {
            weaponController.OnTriggerHold(); //run WeaponController's "OnTriggerHold" function
        }

        if (Input.GetMouseButtonUp(0))
        {
            weaponController.OnTriggerRelease(); //run WeaponController's "OnTriggerRelease" function
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            weaponController.Reload(); //run WeaponController's "Reload" function
        }

        if (transform.position.y <= -10) //if player fall out the map
        {
            TakeDamage(health);          //player die
        }

        if (Input.GetKeyDown(KeyCode.Escape) && sceneName == "Game") //if player press escape at game scene
        {
            Cursor.visible = true;                                   //set cursor to visible
            SceneManager.LoadScene("Menu");                          //return to menu
        }
    }
예제 #5
0
    // 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();
        }
    }
예제 #6
0
    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);
            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);
            }
            //print((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).magnitude);
        }

        //Weapon
        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);
        }
    }
예제 #7
0
    // Update is called once per frame
    void Update()
    {
        ////////////////////
        //Movement input
        ////////////////////

        Vector3 moveInput    = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); //GetAxisRaw is good!
        Vector3 moveVelocity = moveInput.normalized * moveSpeed;

        controller.Move(moveVelocity);

        ////////////////////
        //Look input
        ////////////////////

        //Draw a ray from the camera where the mouse is
        Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
        //Create a plane where the ground is. Dont bother getting the plane from the game, too problematic.
        Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight); //First vector tells where the plane should point, second vector is a point the vector should intersect (for tilt)
        float rayDistance;

        if (groundPlane.Raycast(ray, out rayDistance))
        {                                              //If ray intersects with groundplane, return true and give out the distance the ray traveled.
            Vector3 point = ray.GetPoint(rayDistance); //Get the point where the ray intersected the plane
                                                       //Debug.DrawLine(ray.origin, point, Color.red);
            controller.LookAt(point);

            crosshair.transform.position = point;
            crosshair.DetectTargets(ray);

            //print(new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)); //Check the dist between the player and the crosshair
            if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1)
            { //If the crosshair is too close, stop aiming
                gunController.Aim(point);
            }
        }



        ////////////////////
        //Weapon input
        ////////////////////

        if (Input.GetMouseButton(0))
        { //if mouse button is held down
            gunController.OnTriggerHold();
        }
        if (Input.GetMouseButtonUp(0))
        { //if mouse button is let go
            gunController.OnTriggerRelease();
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            gunController.Reload();
        }

        ////////////////////
        //Menu input
        ////////////////////
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.visible = !Cursor.visible;
        }

        //Kill the player if they fall off
        if (transform.position.y < -10)
        {
            TakeDamage(health);
        }
    }
예제 #8
0
    void Update()
    {
        // Movement Input
        Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

        Vector3 moveVelocity = moveInput.normalized * moveSpeed;

        controller.Move(moveVelocity);

        if (moveInput.x == 1)
        {
            if (moveInput.z == 1)
            {
                tankBody.eulerAngles = new Vector3(0, 45, 0);
            }
            else if (moveInput.z == 0)
            {
                tankBody.eulerAngles = new Vector3(0, 90, 0);
            }
            else if (moveInput.z == -1)
            {
                tankBody.eulerAngles = new Vector3(0, 135, 0);
            }
        }
        else if (moveInput.x == 0)
        {
            if (moveInput.z == 1)
            {
                tankBody.eulerAngles = new Vector3(0, 0, 0);
            }
            else if (moveInput.z == -1)
            {
                tankBody.eulerAngles = new Vector3(0, 180, 0);
            }
        }
        else if (moveInput.x == -1)
        {
            if (moveInput.z == 1)
            {
                tankBody.eulerAngles = new Vector3(0, 315, 0);
            }
            else if (moveInput.z == 0)
            {
                tankBody.eulerAngles = new Vector3(0, 270, 0);
            }
            else if (moveInput.z == -1)
            {
                tankBody.eulerAngles = new Vector3(0, 225, 0);
            }
        }

        // 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);
            crosshair.transform.position = point;
            crosshair.DetectTargets(ray);
            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 (Input.GetKeyDown(KeyCode.Tab))
        {
            gunNumber++;
            if (gunNumber >= guns.Length)
            {
                gunNumber = 0;
            }
            gunController.EquipGun(guns[gunNumber]);
        }

        if (transform.position.y < -5)
        {
            TakeDamage(health);
        }
    }
예제 #9
0
 public void DetectTargets(Ray ray)
 {
     crosshair.DetectTargets(ray);
 }