Search Unity

Selecting a game object through the screen

Discussion in 'Scripting' started by chippy-cheese, Apr 15, 2012.

  1. chippy-cheese

    chippy-cheese

    Joined:
    Feb 29, 2012
    Posts:
    124
    I want to to have a script where i want to make a target. I need to select it through the screen by clicking on the object through a script on my main person and not the other guy I have a collider on thing I'm trying to select.
     
  2. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    You need to raycast to your target, the result of which will return your targets gameobject.
     
  3. chippy-cheese

    chippy-cheese

    Joined:
    Feb 29, 2012
    Posts:
    124
    ok im trying this script but it is giving me errors and not even picking up unless my guy is facing him

    if(Input.GetMouseButton(0)){
    Vector3 fwd = transform.TransformDirection(Vector3.forward);
    if (Physics.Raycast(Camera.current.transform.position, fwd, 100))
    print("There is something in front of the object!");
    Debug.DrawRay(Camera.current.transform.position, fwd, Color.white,0.01f);
    }
     
  4. chippy-cheese

    chippy-cheese

    Joined:
    Feb 29, 2012
    Posts:
    124
    just to let you know this camera doesn't really rotate like a fps but instead its a 3rd person camera
     
  5. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    I use this to select objects:

    Code (csharp):
    1.  
    2. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3. RaycastHit hit;
    4. if (Physics.Raycast(ray, out hit, 1000))
    5.        {
    6.            GameController.selection = hit.transform.gameObject;
    7.            print(GameController.selection);
    8.            cam.target = hit.transform;
    9.        }
    10.  
    Hope it helps. :)
     
  6. chippy-cheese

    chippy-cheese

    Joined:
    Feb 29, 2012
    Posts:
    124
    where does the GameController come from?
     
  7. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    Ignore camecontroller, that's particular to his project... your target is hit.transform.object, so use it however you like.
     
  8. chippy-cheese

    chippy-cheese

    Joined:
    Feb 29, 2012
    Posts:
    124
    ok i tried doing what you've said but still nothing works for me. This has got to be one of the most confusing things I've done so far.
     
  9. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Chippy,

    Could you use the OnMouseDown function, something like this? First make a small script like this, call it something like "PickTarget" and attach it to the object you want to be the target (which has to have a collider, like a cube for example).
    Code (csharp):
    1. var targetPicked : String;
    2.  
    3. function OnMouseDown () {
    4.     targetPicked = "target picked";
    5. }
    Then add something like this to your main person script:
    Code (csharp):
    1. private var makeTarget : GameObject;
    2. private var foundTarget : String;
    3.  
    4. function Start () {
    5.     makeTarget = GameObject.Find("target");
    6. }
    7.  
    8. function Update () {
    9.     foundTarget = makeTarget.GetComponent(PickTarget).targetPicked;
    10.     Debug.Log (foundTarget);
    11.    
    12. }
    This works by referencing the small script you added to the target object from within the Main Person script by first finding it,
    Code (csharp):
    1. makeTarget = GameObject.Find("target");
    2. }
    and then referencing the variable "targetPicked" you made in that little script.
    Code (csharp):
    1. foundTarget = makeTarget.GetComponent(PickTarget).targetPicked;
    Zaffer
     
    Last edited: Apr 16, 2012