Search Unity

Question Need Help with a Projectile script I made

Discussion in 'Scripting' started by AydanG, Jul 8, 2020.

  1. AydanG

    AydanG

    Joined:
    Jun 30, 2020
    Posts:
    3
    I made an enemy AI script that follows, retreats, and shoots at my player, but for some reason, the Game Object(bullet) it spawns in always has a rotation that is (0, 0, 0) and faces upwards because of it. The object that I am trying to spawn in, however, is rotated in the prefab. Can anyone help with this?

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class EnemyAI : MonoBehaviour
    10.  
    11. {
    12.  
    13.  
    14.  
    15.     public float speed;
    16.  
    17.     public float stoppingDistance;
    18.  
    19.     public float retreatDistance;
    20.  
    21.  
    22.  
    23.     public GameObject bullet;
    24.  
    25.     public Transform player;
    26.  
    27.  
    28.  
    29.     private float timeBtwShots;
    30.  
    31.     public float startTimeBtwShots;
    32.  
    33.  
    34.  
    35.     // Start is called before the first frame update
    36.  
    37.     void Start()
    38.  
    39.     {
    40.  
    41.         player = GameObject.Find("Player").transform;
    42.  
    43.  
    44.  
    45.         timeBtwShots = startTimeBtwShots;
    46.  
    47.     }
    48.  
    49.  
    50.  
    51.     // Update is called once per frame
    52.  
    53.     void Update()
    54.  
    55.     {
    56.  
    57.         if (Vector3.Distance(transform.position, player.position) > stoppingDistance)
    58.  
    59.         {
    60.  
    61.             transform.position = Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    62.  
    63.         }
    64.  
    65.         else if (Vector3.Distance(transform.position, player.position) < stoppingDistance && Vector3.Distance(transform.position, player.position) > retreatDistance)
    66.  
    67.         {
    68.  
    69.             transform.position = this.transform.position;
    70.  
    71.         }
    72.  
    73.         else if (Vector3.Distance(transform.position, player.position) < retreatDistance)
    74.  
    75.         {
    76.  
    77.             transform.position = Vector3.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
    78.  
    79.         }
    80.  
    81.         if (timeBtwShots < -0)
    82.  
    83.         {
    84.  
    85.             Instantiate(bullet, transform.position, Quaternion.identity);
    86.  
    87.             timeBtwShots = startTimeBtwShots;
    88.  
    89.         }
    90.  
    91.         else
    92.  
    93.         {
    94.  
    95.             timeBtwShots -= Time.deltaTime;
    96.  
    97.         }
    98.  
    99.  
    100.  
    101.     }
    102.  
    103. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You're telling Unity to give your object a rotation of (0,0,0) when you pass in Quaternion.identity for the rotation value.
     
    Kurt-Dekker likes this.
  3. AydanG

    AydanG

    Joined:
    Jun 30, 2020
    Posts:
    3
    What can i use instead?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Whatever your intended rotation for the new object is? You can use the rotation from the prefab if you want.
     
  5. AydanG

    AydanG

    Joined:
    Jun 30, 2020
    Posts:
    3
    Sry, im not really too familiar with unity, so im not sure what to put in place of it, would you happen to know what I could put