Search Unity

Shoot where my character is facing.

Discussion in '2D' started by Pnm279, Apr 28, 2016.

  1. Pnm279

    Pnm279

    Joined:
    Apr 28, 2016
    Posts:
    7
    Hi all!!

    Im new with this tool and im making a 2d platformer game, where i can make my char shot lasers to where he is looking at.

    This is the script for him to shoot:

    --------------------------------------------------------------------------------------------------------------
    using UnityEngine;
    using System.Collections;

    public class Dispara : MonoBehaviour {
    public float fireRate = 5;
    private float timeToFire = 0;
    Transform Ojos;
    public Transform Laser;

    // Use this for initialization
    void Awake()
    {
    Ojos = transform.FindChild("Ojos");

    }
    // Use this for initialization
    void Start () {



    }

    // Update is called once per frame
    void Update () {
    if (Input.GetButton("Fire1") && Time.time> timeToFire)
    {
    timeToFire = Time.time + fireRate;
    Shoot();
    }
    }

    public void Shoot()
    {
    Instantiate(Laser, Ojos.position, Ojos.rotation);

    }
    }
    --------------------------------------------------------------------------------------------------------------------
    This succesfully creates an instance of "Laser" wich i have created, and in laser i created a script wich on start would give it a speed:

    ---------------------------------------------------------------------------------------------------------------------

    using UnityEngine;
    using System.Collections;

    public class Laser : MonoBehaviour {


    private Dispara direccionMira; //Here im accesing the script above.
    private Vector2 direccion;
    Transform coli;
    void Awake()
    {
    direccionMira = new Dispara();//ERROR?
    direccion = direccionMira.GetComponent<GameObject>().transform.forward;
    coli = transform.FindChild("Colision");
    }
    void FixedUpdate()
    {




    coli.GetComponent<Rigidbody2D>().velocity = direccionMira.GetComponent<GameObject>().transform.forward * 6;

    Invoke("Destruir",5);


    }

    void Destruir()
    {
    Destroy(gameObject,2);


    }




    }

    --------------------------------------------------------
    There are 2 problems with this, it tells me first that i can create a "new" monobehaviour instance of class Dispara.

    And bullet is not going to where the character is facing.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Please use code tags when posting snippets of code: http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    To address one point in your post:
    MonoBehaviours cannot be created using the "new" keyword. They are Components and must be added to a GameObject.

    You can either create a new GameObject and add the component, or make a prefab with the component already added, then Instantiate the prefab.

    So without creating the prefab and using Instantiate, you would have to do this:
    Code (CSharp):
    1. private Dispara direccionMira; //Here im accesing the script above.
    2. private Vector2 direccion;
    3. Transform coli;
    4. void Awake() {
    5.     direccionMira = new GameObject("NewDisparaGameObject",typeof(Dispara)).GetComponent<Dispara>();
    6.     direccion = direccionMira.transform.forward; // note: you can get transform directly from any monobehaviour or gameobject
    7.     coli = transform.FindChild("Colision");
    8. }
     
    Last edited: Apr 29, 2016