Search Unity

Projectile always firing above player, not directly at.

Discussion in 'Scripting' started by jwalden, Jun 12, 2018.

  1. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    62
    Hi guys, so a projectile instantiating from an enemy is always firing above my player, and not directly at.

    I've checked that all my transform positions are centred (especially for the gameobject that is being referenced)

    The relevant code is below

    Code (CSharp):
    1.  
    2.  
    3. private GameObject _player;
    4. public float projSpeed;
    5. private Vector3 direction;
    6.  
    7. void Start ()
    8.     {
    9.         projSpeed *= Time.deltaTime;
    10.         _player = GameObject.Find("_PlayerMove");
    11.         StartCoroutine(FirePlayer());
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.  
    18.         direction = (_player.transform.position - transform.position).normalized;
    19.  
    20.     }
    21.  
    22. private IEnumerator FirePlayer()
    23.     {
    24.  
    25.         while (true)
    26.         {
    27.             yield return new WaitForSeconds(1);
    28.            
    29.             GameObject shot1 = Instantiate(_projectile, _shootOrigin.transform.position, Quaternion.LookRotation(direction));
    30.             shot1.GetComponent<Rigidbody>().velocity = direction * projSpeed;
    31.         }
    32.     }
    Any ideas what the problem might be?
     
  2. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    Can you share any video or gif ?