Search Unity

Directing a fireball

Discussion in 'Scripting' started by tawdry, Jan 29, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Hi guys
    Trying to direct a fireball at a target but it either goes the opposite direction or fires everywhichway.
    So i have a empty gameobject on the point of a staff I fire a prefab "fireball" from it but don't know how to write it that it goes in the direction of the enemy. It has its own velocity so just needs direction heres what i tried and failed with.

    Code (CSharp):
    1.  
    2.             transform.position = Vector3.MoveTowards(transform.position, target.position,1);//no effect
    3.             Instantiate (fireball, transform.position, transform.rotation);//everywhichway
    4.             Instantiate (fireball, transform.position, target.rotation)     //goes in the opposite direction  ;
    Also why does this not work
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. p
    5.     public GameObject Night;{
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.     //    Night = GameObject.Find ("batty");
    10. Debug.Log(Night.name);    
    11.   knight = Night.GetComponent<Animator>();
    12.         him = Night.GetComponent<NavMeshAgent>();
    13.         //target = night.transform;
    14.  
    15.     }
    16.  
    17.  
    18.  
    When i assign the GO batty to the public Night it doesn't work but with this line Night = GameObject.Find ("batty"); It does work the debug log Night.name returns "batty" for both so it should work for both?
     
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    To your first question: Try this:

    Code (CSharp):
    1. Vector3 direction = target.position - transform.position;
    2. Quaternion rotation = Quaternion.LookRotation(direction);
    3.  
    4. Instantiate(fireball, transform.position, rotation);
    To your second question:

    What do you mean by it is not working? What is supposed to happen?
     
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Any chance you are dragging in a prefab for the second part rather than an instantiated object from the scene? Name will work fine for prefabs, but you need an actual instance of the object to work with its components.
     
  4. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Thx sir that worked perfect. As for the second part well can't get any of the info from "batty". He is in the scene from the start but placing him as a public GO does not allow me access to his components(at least it does not let me effect change to his components) unless i do the find routine as well.