Exemple #1
0
 private void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         tankShooting.Fire();
     }
 }
 public void tankFire()
 {
     if (!GamePause.isPause)
     {
         firer.Fire();
     }
 }
Exemple #3
0
    public bool moveAgent(AbstractGOAPAction nextAction)
    {


        if (path != null && path.Count > 0)
        {
            currentTargetNode = path[0];
            if (Vector3.SqrMagnitude(transform.position - currentTargetNode.transform.position) <= 1.0f)
            {
                path.RemoveAt(0);
            }

        }
        if (currentTargetNode != null)
        {
            transform.position = Vector3.MoveTowards(transform.position, new Vector3(currentTargetNode.transform.position.x, transform.position.y,
                currentTargetNode.transform.position.z), Time.deltaTime * 2);

            // Gets vector direction of movement, normalize and multiply it with speed after setting the y direction to 
            Vector3 posValue = currentTargetNode.transform.position - transform.position;

            // Move towards goal
            if (currentTargetNode != null && posValue.magnitude > minDist)
            {
                posValue.y = 0;

                Vector3 newDir = Vector3.RotateTowards(transform.forward, posValue, 0.1f, 0.0f);
                transform.rotation = Quaternion.LookRotation(newDir);
            }

        }
        // Sends a raytrace to check if the enemy tank is in view
        RaycastHit hit;
        bool tankVisible = !Physics.Linecast(transform.position, EnemyTank.transform.position, out hit, ~(1 << gameObject.layer));

        canSeeEnemy = tankVisible;
        Debug.DrawLine(transform.position, EnemyTank.transform.position, Color.green);
        Debug.DrawRay(barrelDirection.position, barrelDirection.forward * 100, Color.blue);    // DEBUG

        if (tankVisible || hit.collider.transform == EnemyTank.transform)
        {
            knownEnemyPosition = new Vector3(EnemyTank.transform.position.x, 0, EnemyTank.transform.position.z);
            // check if barrel points towards target
            RaycastHit barrelHit;
            // Only shoots if the enemy is in sights from the turret
            if (Physics.Linecast(transform.position, transform.position + barrelDirection.forward * 100, out barrelHit, (1 << gameObject.layer))) //|| barrelHit.collider.transform == EnemyTank.transform)
            {
                shoot.Fire();
            }

        }
     

        // Points the turrent towards the last known position of the tank as long as one such position is known
        if (knownEnemyPosition != null)
        {
            PointTurretAtTarget();
        }
        return true;
    }
Exemple #4
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (path != null && path.Count > 0)
        {
            currentTargetNode = path[0];
            if (Vector3.SqrMagnitude(transform.position - currentTargetNode.transform.position) <= 1.0f)
            {
                path.RemoveAt(0);
            }
        }
        transform.position = Vector3.MoveTowards(transform.position, new Vector3(currentTargetNode.transform.position.x, transform.position.y,
                                                                                 currentTargetNode.transform.position.z), Time.deltaTime * 2);
        // Gets vector direction of movement, normalize and multiply it with speed after setting the y direction to 0

        Vector3 posValue = currentTargetNode.transform.position - transform.position;

        // Move towards goal
        if (currentTargetNode != null && posValue.magnitude > minDist)
        {
            posValue.y = 0;

            Vector3 newDir = Vector3.RotateTowards(transform.forward, posValue, 0.1f, 0.0f);
            transform.rotation = Quaternion.LookRotation(newDir);
        }

        // If a major goal is chosen, the tank is no longer moving andthe tank have reached its major goal
        else if (endTargetNode != null && (endTargetNode.transform.position - transform.position).magnitude < minDist)
        {
            Debug.Log("Goal " + currentTargetNode.name + " reached");
            currentTargetNode = endTargetNode = null;
        }

        // Sends a raytrace to check if the enemy tank is in view
        RaycastHit hit;
        bool       tankVisible = !Physics.Linecast(transform.position, new Vector3(EnemyTank.transform.position.x, 0, EnemyTank.transform.position.z), out hit, ~(1 << gameObject.layer));

        Debug.DrawRay(barrelDirection.position, barrelDirection.forward * 100, Color.blue);    // DEBUG

        if (tankVisible || hit.collider.transform == EnemyTank.transform)
        {
            knownEnemyPosition = new Vector3(EnemyTank.transform.position.x, 0, EnemyTank.transform.position.z);
            // check if barrel points towards target
            RaycastHit barrelHit;

            // Only shoots if the enemy is in sights from the turret
            if (Physics.Linecast(transform.position, transform.position + barrelDirection.forward * 100, out barrelHit, (1 << gameObject.layer))) //|| barrelHit.collider.transform == EnemyTank.transform)
            {
                shoot.Fire();
            }
        }

        // Points the turrent towards the last known position of the tank as long as one such position is known
        if (knownEnemyPosition != null)
        {
            PointTurretAtTarget();
        }
    }
Exemple #5
0
 private void Fire()
 {
     Debug.Log("DistanceToPlayer: " + DistanceToPlayer);
     if (DistanceToPlayer < 20f)
     {
         shootScript.Fire(15f);
     }
     else if (DistanceToPlayer < 30f)
     {
         shootScript.Fire(20f);
     }
     else if (DistanceToPlayer < 40f)
     {
         shootScript.Fire(25f);
     }
     else
     {
         shootScript.Fire(30f);
     }
 }
    private bool Fire(float chance)
    {
        float fire = Mathf.Clamp(chance, -1, 1);

        if (fire > 0)
        {
            tankShooting.Fire(true, this);
            return(true);
        }

        return(false);
    }
Exemple #7
0
    private void HandleTurret(float turretAction)
    {
        int index = Mathf.FloorToInt(turretAction);

        switch (index)
        {
        case 0:
            tankShooting.AddTurn(-1);
            break;

        case 1:
            tankShooting.AddTurn(1);
            break;

        case 2:
            tankShooting.Fire(true, this);
            break;
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (alive)
        {
            navMeshAgent.SetDestination(enemyAgent.transform.position);
            if (navMeshAgent.remainingDistance > 3.5f)
            {
                navMeshAgent.isStopped = false;
            }
            else
            {
                navMeshAgent.isStopped = true;
            }

            distanceToEnemy = Mathf.Abs((transform.position - enemyAgent.transform.position).magnitude);
            if (distanceToEnemy < 10 && tankShooting.canFire)
            {
                tankShooting.Fire(distanceToEnemy);
            }
        }
        else
        {
            if (!scored)
            {
                ResetAgent(0);
            }
        }
        if (enemyAgent.died)
        {
            if (!scored)
            {
                ResetAgent(1);
            }
        }
        //if (enemyAgent.tankHealth.m_Dead) if (!scored) ResetAgent(1);
    }
    public override void AgentAction(float[] vectorAction, string textAction)
    {
        AddReward(-0.0001f);
        if (died)
        {
            //Only used when normal speed
            if (Time.timeScale == 1)
            {
                ScoreIt(0);
            }

            Done();
        }
        else if (enemyKilled)
        {
            //Only used when normal speed
            if (Time.timeScale == 1)
            {
                ScoreIt(1);
            }

            SetReward(1);
            Done();
        }

        //Actions
        int moveForward = (int)vectorAction[0];

        if (moveForward == 1)
        {
            Vector3 movement = transform.forward * speed * Time.deltaTime;
            rigid.MovePosition(rigid.position + movement);
        }
        else if (moveForward == 2)
        {
            Vector3 movement = transform.forward * -speed * Time.deltaTime;
            rigid.MovePosition(rigid.position + movement);
        }

        int rotation = (int)vectorAction[1];

        if (rotation == 1)
        {
            transform.Rotate(Vector3.up * rotationSpeed, Space.World);
        }
        else if (rotation == 2)
        {
            transform.Rotate(Vector3.up * -rotationSpeed, Space.World);
        }

        float distanceToTarget = Vector3.Distance(this.transform.position, Target.position);

        int shoot = (int)vectorAction[2];


        if (shoot == 1 && tankShooting.canFire)
        {
            float launchForce = distanceToTarget * forceMultiplier;
            tankShooting.Fire(launchForce);
        }

        if (transform.position.y > 1 || transform.position.y < 0)
        {
            died = true;
        }
    }
Exemple #10
0
 public void tankFire()
 {
     playerFireScript.Fire();
 }