Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question My enemy is shooting the sphere behind him!!!

Discussion in 'Getting Started' started by notjake85, May 2, 2024.

  1. notjake85

    notjake85

    Joined:
    Feb 20, 2024
    Posts:
    45
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.AI;
    5.  
    6. public class EnemyFollow2 : MonoBehaviour
    7. {
    8.     public UnityEngine.AI.NavMeshAgent enemy;
    9.  
    10.     public Transform player;
    11.  
    12.     public LayerMask Ground, whatIsPlayer;
    13.  
    14.     //Patroling
    15.     public Vector3 walkPoint;
    16.     bool walkPointSet;
    17.     public float walkPointRange;
    18.  
    19.     //Attacking
    20.     public float timeBetweenAttacks;
    21.     bool alreadyAttacked;
    22.     public GameObject projectile;
    23.  
    24.     //States
    25.     public float sightRange, attackRange;
    26.     public bool playerInSightRange, playerInAttackRange;
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.        
    31.     }
    32.     private void Awake()
    33.     {
    34.         player = GameObject.Find("Player").transform;
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.         enemy.SetDestination(player.position);
    41.  
    42.         //Check for sight and attack range
    43.         playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
    44.         playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
    45.  
    46.         if (!playerInSightRange && !playerInAttackRange) Patroling();
    47.         if (playerInSightRange && !playerInAttackRange) ChasePlayer();
    48.         if (playerInSightRange && playerInAttackRange) AttackPlayer();
    49.     }
    50.    
    51.     private void Patroling()
    52.     {
    53.         if (!walkPointSet) SearchWalkPoint();
    54.  
    55.         if (walkPointSet)
    56.             enemy.SetDestination(walkPoint);
    57.  
    58.         Vector3 distanceToWalkPoint = transform.position - walkPoint;
    59.  
    60.         //Walkpoint reached
    61.         if (distanceToWalkPoint.magnitude < 1f)
    62.             walkPointSet = false;
    63.     }
    64.  
    65.     private void SearchWalkPoint()
    66.     {
    67.         //Calculate random point in range
    68.         float randomZ = Random.Range(-walkPointRange, walkPointRange);
    69.         float randomX = Random.Range(-walkPointRange, walkPointRange);
    70.  
    71.         walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
    72.        
    73.         if (Physics.Raycast(walkPoint, -transform.up, 2f, Ground))
    74.             walkPointSet = true;
    75.     }
    76.  
    77.     private void ChasePlayer()
    78.     {
    79.         enemy.SetDestination(player.position);
    80.     }
    81.  
    82.     private void AttackPlayer()
    83.     {
    84.         //Enemy doesn't move
    85.         enemy.SetDestination(transform.position);
    86.  
    87.         transform.LookAt(player);
    88.  
    89.         if (!alreadyAttacked)
    90.         {
    91.             Rigidbody rb = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Rigidbody>();
    92.  
    93.             rb.AddForce(player.position * 200f, ForceMode.Impulse);
    94.             rb.AddForce(transform.up * 0.001f, ForceMode.Impulse);
    95.            
    96.             alreadyAttacked = true;
    97.             Invoke(nameof(ResetAttack), timeBetweenAttacks);
    98.         }
    99.     }
    100.  
    101.     private void ResetAttack()
    102.     {
    103.         alreadyAttacked = false;
    104.     }
    105. }
    106.  
    I dont have any errors but is there any reason he might be doing this?
    hes looking at me too but the sphere comes out his but almost and shoots behind him.
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,166
    well id guess rb.AddForce(player.position * 200f, ForceMode.Impulse); player.position being used as a direction.. its not a direction
     
  3. notjake85

    notjake85

    Joined:
    Feb 20, 2024
    Posts:
    45
    should i use transform.forward?