Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  3. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

worldtoscreenpoint not working HELP plz

Discussion in 'Community Learning & Teaching' started by deusxlr, Jan 12, 2018.

  1. deusxlr

    deusxlr

    Joined:
    Jan 12, 2018
    Posts:
    1
    I have a problem in my code please someone help, it is like a code to select units in scene, but it gives an error in the "WorldToScreenPoint" it seems that my copilator does not read it.
    maybe that extension does not exist in the new camera of unity, I was thinking of trying to replace with a "comparetag" or something.
    This code is not mine, I copied it from a face on you tube, it worked for him. but he had an old version of unity



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    public class selecaounidades : MonoBehaviour {

    private Vector2 posicaoinicial;
    private Vector2 finalposition;
    public Texture2D retangulo;
    private List<basepersoangens> unidadesemcena;// aqui criamos uma lista encadeada da classe basepersoangens
    public Camera cameraprincipal;


    private void Start()
    {
    unidadesemcena = new List<basepersoangens>();
    cameraprincipal= GetComponent<Camera>();
    }

    private basepersoangens[] GetUnitsUnderRectangle (Rect selectionRectangle)
    {
    List<basepersoangens> selectedunits = new List<basepersoangens >();

    foreach (basepersoangens unit in unidadesemcena)
    {
    Vector3 unitemcampo = cameraprincipal.WorldToScreenPoint(unit.transform.position);
    Vector2 convertedunit = new Vector2(unitemcampo.x,Screen .height - unitemcampo.y );
    if (selectionRectangle.Contains (convertedunit ))
    {
    selectedunits.Add(unit);
    }


    }
    return selectedunits.ToArray();
    }

    public void adicionaabaselista(basepersoangens unidade)
    {
    unidadesemcena .Add(unidade);
    }

    void OnGUI()
    {
    if (Input.GetButtonDown("Fire1"))
    {
    posicaoinicial = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
    }
    if (Input.GetButton ("Fire1"))// aqui se faz a caixa
    {
    Vector2 finalposition = new Vector2(Input.mousePosition.x,Screen .height - Input.mousePosition.y);
    GUI.Box(new Rect (posicaoinicial .x,posicaoinicial .y, finalposition.x - posicaoinicial .x, finalposition.y - posicaoinicial .y),"");
    }
    if(Input.GetButtonUp ("Fire1"))
    {
    float xmin = Mathf.Min(posicaoinicial.x, finalposition.x);
    float ymin = Mathf.Min(posicaoinicial.y, finalposition.y);

    float width = Mathf.Abs(posicaoinicial.x- finalposition.x);
    float height = Mathf.Abs(posicaoinicial.y - finalposition.y);
    basepersoangens[] selecteunits = GetUnitsUnderRectangle(new Rect(xmin, ymin, width, height));
    }

    }


    }