#Object selector on OnMouseOver

1 messages · Page 1 of 1 (latest)

stable solstice
#

hello

molten bramble
#

hi

stable solstice
#

ok show me your script you wrote?

#

Object selector on OnMouseOver

molten bramble
#
public void OnMouseOver()
    {
        if (Input.touchCount > 0 && tap.trigger == true)
        {
            if (gm.letterCount == 0 && Tap.drop == false)
            {
                boxField.text += circleValue.GetComponent<TMPro.TextMeshProUGUI>().text;
                boxFields[0].text = circleValue.text;
                GM.playerGuess += circleValue.GetComponent<TMPro.TextMeshProUGUI>().text;
                gm.letterCount++;
                Debug.Log(GM.playerGuess);
                GetComponent<Collider2D>().enabled = false;
            }
        }
    }
}

#

thats what i had on the object, but i want to move it to another scirpt

stable solstice
#

why not using ray cast to get the selected object directly

molten bramble
#

idk whats that:(

stable solstice
#

raycast is a ray which gives which gameobject is chosen and you can get the data of that object over which the raycast hits

molten bramble
#

using things which my brain can think of

stable solstice
#

ray cast can also be controlled by mouse click

molten bramble
#

here is a problem

#

i dont have a click

stable solstice
#

you dont need a void OnMouseDown

#

here first read how raycast works

#

i dont think if its that much hard to understand!

#

@molten bramble

#

??

molten bramble
#

thinking/ googling etc:D

stable solstice
molten bramble
#

how to determine which object raycast will hit?

stable solstice
#

I know what you need cuz i was working on the same thing

stable solstice
#

ray cast is just a ray

molten bramble
#

and if there is no mouse clicks exist?

stable solstice
#

it can return the gameobject on which the raycast hit

stable solstice
#

if its zoom then individual touches are required

molten bramble
#

will it work if i want it to trigger all the time while player holds finger on the screen

stable solstice
#

it will work like the object you click on

#

but you can modify it as you need

molten bramble
#

onmouseover was super neat way to do so

#

just attach script to an object and its ready pretty much

stable solstice
#

ray cast can work like this to return a game object you select

#
            {
                {
                    selectedObject = hit.transform.gameObject;
                    Debug.Log(selectedObject);
                }
            }```
#

here we got a ray and named its output as hit

#

now the selectedObject here is a public GameObject

#

Public GameObject selectedObject;

#

the gameobject can be setup as the object on which the ray cast hit

#

now you have the code on how to select the object

#

with raycast

#

but do you know how to set the direction of raycast with mouse click?

#

RaycastHit hit;

#

i forgot to mention the raycast lol

molten bramble
#

i will assume i have to put origin point

#

and from there it will draw a ray to my object?

stable solstice
#

set the raycast direction on where you click

stable solstice
#

raycast hits from your camera

#

you can also set its direction with mouse click

#

now think how?

molten bramble
#

vector

stable solstice
#

here this will help you

#

read and tell me when the guy wrote the raycast what it did next to get the input

molten bramble
#

ScreenPointToRay

#

the only thing which is somehow can be related to dirction

stable solstice
#

screen point to ray is to get the ray point emitted from the screen you have

#

like the camera

#

what did it do to get the direction of the raycast

#

here

#

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

#

(Input.mousePosition)

#

that,s the thing that specifies the direction of raycast

#

now assemble the code and make a script to select the gameobject with raycast

molten bramble
#

ye, i was writing about this line of code

stable solstice
#

remember to write the function inside the void update()

#

if you get a little misunderstanding in writing the code then here is a hint

#
    {
        if (Input.GetMouseButtonDown(0))
        {
                ///your method here
        }
    }```
#

show me your code when its done?

molten bramble
#

yep yep, figuring it out:)

stable solstice
#

try it

#

and ask me out if you need help in anything

#

sorry for my

#

it is my first time teaching a person like this lol

#

so i might be bad to let you understand things

#

so did you figure out a way to write the code @molten bramble

#

?

molten bramble
#

got this rn

stable solstice
#

but you forgot one thing

#

do it in a mouse input

#

there is no thing like that

#

write it in this if statement

#
        {
                ///your method here
        }```
molten bramble
stable solstice
#

yea its good now

#

try it now

#

it will work now

#

but update void was more necessary to do it

#

to get the void work on every frame

#

anyways try it

#

it should work

molten bramble
#

nope

#

it doesnt work

#

it doenst pass this if

stable solstice
#

it works on an Update void

#

void Update() {}

molten bramble
#

everything before that works

#

like nnm debuglog and et

stable solstice
molten bramble
#

maybe object needs something too?

#

i have only 2d colliders on them

stable solstice
#

it works fine for me

#

wait lemme send you the complete script

molten bramble
#

tried your code

#

didnt work too

#

seems like problem is the object)

stable solstice
#

try again or consult someone

#

cuz it works fine to me

stable solstice
#

did you us the script on camera or something else?

#

@molten bramble

#

use it on camera

#

due to this thing

#

Camera.main.ScreenPointToRay

#

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

#

now it will do things for you

#

😅 I got busy so i couldnt respond

molten bramble
#

Aaaah that’s the issue

#

Will have to test it later

#

A bit busy rn

molten bramble
#

@stable solstice figured it ouy

#

problem is it required 3d collider:D

molten bramble
#

or i should use 2d raycast)

stable solstice
#

so dont use that

#

but depends on your choice for what you need

#

anyways i am gonna leave this thread so bye!