Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make an Object Face Forward

Discussion in 'Scripting' started by RaGEn, Jan 31, 2016.

  1. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    I'm trying to make a gameobject in my game fire bullets at the player. However whenever the gameobject fires the bullets they always facing the same direction. I need to get them to face forward to make it look like their flying straight. Anyone know any ways to make the bullets face forward no matter what? Or at least have them looking at the target that was being shot at?

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (Target != null)
    4.         {
    5.             transform.parent.LookAt (Target.transform);
    6.             ShootTime -=Time.deltaTime;
    7.         }
    8.  
    9.         if (ShootTime <0)
    10.         {
    11.             Instantiate(Bullets, gameObject.transform.position, gameObject.transform.rotation);
    12.             ShootTime = ShootNumber;
    13.         }
    14.     }
    15.    
    16.     void OnTriggerEnter (Collider other)
    17.     {
    18.         if (other.gameObject.CompareTag ("GoldTeam"))
    19.         {
    20.             Target = other.gameObject;
    21.         }
    22.     }
    That's my gameobject spawning the bullets.

    Code (CSharp):
    1. void Awake ()
    2.     {
    3.         GetComponent<Rigidbody>().velocity = transform.forward * speed;
    4.     }
    5.    
    6.     void FixedUpdate ()
    7.     {
    8.  
    9.         transform.rotation = Quaternion.Euler(90, 0, 0);
    10.         Life -= Time.deltaTime;
    11.        
    12.         if (Life <= 0)
    13.         {
    14.             Destroy(gameObject);
    15.         }
    16.     }

    That's my bullet movement.
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    RaGEn likes this.
  3. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88