Search Unity

clicking 3D objects (fixed)

Discussion in 'Scripting' started by mattcscz, Mar 7, 2010.

  1. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    hi, i just want to try somthing simple, which is if i click a 3D cube for it to print somthing. can anyone write me a short line of code so i can learn from it? thanks
     
  2. Theformand

    Theformand

    Joined:
    Jan 2, 2010
    Posts:
    271
    If you have a cube in your scene, you want to check out ScreenPointToRay and Raycasting in the scripting reference.

    heres the basic thing you want to do in pseudo code.

    Cast ray into scene.
    Ray emits from Mouse.Position
    if(ray hit == cube (this could be a tag, or by gameObject name)
    {print ("woot")}
     
  3. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    thanks man, so i would do this in the function update?
    so:

    function Update () {
    if(ray hit == cube)
    {print ("woot")}}

    thanks
     
  4. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    or in a collision function ?

    function OnCollision(collide : Hit) {
    function Update () {
    if(ray hit == cube)
    {print ("woot")}}

    ??

    im not so good with this :D
     
  5. dbp

    dbp

    Joined:
    Mar 3, 2010
    Posts:
    324
    you could also attach a script to the cube and use the OnMouseDown function, but that depends on your use-case

    example:

    function OnMouseDown() {
    print("woot");
    }