how do i write the event part

So here is my code so far do the raycast


and i have run a test i can find the object. when i click i can do something.

but then i want to write to event thing to transform the cube maybe move left right up down base on the face that my raycast.


so i know i should write something to send out the job to the new script.
let new script to do the things and send it back to this script.
and i wanna to know how should i write?

i mean i want to know how i can send out the job to the other script?

From the RaycastHit you can get the collider.

From the collider you can call .GetComponent() to get any particular script off the thing you raycasted into.

Be sure to test what you get back for being null before blindly using it.

For future reference:

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

2 Likes

Here is my script. can you show me a example.
and also about the GetComponent, is it only the only cube(object) or all Obj in the tag can work on it?

private float range = 5;
    public delegate void ClickAction();

    public static event ClickAction OnClicked;
    private void Update()
    {
        Vector3 direction = Vector3.forward;
        Ray ray = new Ray(transform.position, transform.TransformDirection(direction * range));
        Debug.DrawRay(transform.position, transform.TransformDirection(direction * range));

        if (Physics.Raycast(ray, out RaycastHit hit, range))
        {
            if (hit.collider.CompareTag("movecube"))
            {
                print("found it Its movecube");
                if (Input.GetMouseButtonDown(0))
                {
                    print("do the event thing");
                   
                }
            }
        }
    }

Rather than me play 21 questions about how your scene is set up, why don’t you start with some basic Youtube tutorials on this stuff instead!

If you can’t get it from the tutorials, you certainly won’t get it from this little text box. It will NOT all be code that you must consider.

The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

This assumes you have at least written and studied some code and have run into some kind of issue.

If you haven’t even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

If you just want someone to do it for you, you need go to one of these places:

https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

Remember this is YOUR game, not ours. We have our own games we’re working on.

You have this backwards. FIRST you check for Input and THEN you check what to do with that Input. The raycast is far more expensive than checking if the mouse button is down. If it isn’t, there’s no need to do any raycasting. :wink:

Moreover, this structure will lead to injecting input tests deep inside your code and likely duplicate them. But the state of input isn’t going to change over the course of a frame. If you ever find yourself writing a line that tests for the exact same input AGAIN you know this needs to be changed to test the input first and use the input state in all other places.

This is very very good practice.

Whatever input you contemplate, make temporary variables to hold it.

private Vector2 movement;
private bool jump;

Now in your Update() loop:

void Update()
{
  // clear input
  movement = Vector2.zero;
  jump = false;

  // gather input
  movement += new Vector2 (Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  if (Input.GetKeyDown( KeyCode.Space))
  {
    jump = true;
  }

  // TODO: any other input: touch, joystick, kinect, etc.

  // TODO: act ONLY on the movement and jump variables, never again touching the actual input system this frame
}

Now Unity comes along and has a “New Input system” that you have to use.

You have like two statements to change. DONE.

1 Like

ok. raycast no need alway here. got it. i will change it click first then raycast then the transform.
i had ask someone on discord he said i can actually to code the tramsform part in same script on update.
so i guess i just not do the send out script part.
all i do this project is want do test is tranform work or not so this part i try it my self so.
if no need to do send out i guess i just skip the unknow part.

Speaking of which here’s the direct workflow for cases where you just need to test something.

if (Mouse.current.leftButton.wasPressedThisFrame)
{
    // do stuff
}
1 Like