Search Unity

How to drop and place an object? Example, patty on stove?

Discussion in 'Scripting' started by MirrorInstinct, Jul 2, 2020.

  1. MirrorInstinct

    MirrorInstinct

    Joined:
    Sep 2, 2019
    Posts:
    41
    Most of what I have been doing is making little projects in order to understand certain scripting techniques for when I do actually make a viable product. As of late I've been making a cooking game project however I've seem to have run into a wall. I am stuck on figuring out how to take a patty and place it on a stove for it to cook.

    The cooking part I can figure out on my own but what I want to know is how do I drag and drop an object onto a stove for it to begin cooking? Is there some sort of "anchor" type script that I have to use in order to accomplish this? Any help with putting together a script is much appreciated. Thank you in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    One thing you need to figure out before diving deeply into any particular solution is the exact behavior you want. So the first question is is this a 2d game or 3d? What's the perspective of the camera relative to the "stove" and wherever the patty is before you drag it?

    If it's a 3d game remember that you will be translating a 2d motion across 2d space (mouse cursor motion across the screen) into a 3d motion, so we will have to do some projection or raycasting from the screen space into world space to figure out what the actual desired position for the object is at any given time.
     
  3. MirrorInstinct

    MirrorInstinct

    Joined:
    Sep 2, 2019
    Posts:
    41
    Good tip! So right now my project is set up as a 2D style in a 3D space as I've noticed a lot of cooking games tend to use this in order to achieve more depth to the visuals. I have already set up the patty to be drag-able so that's not a problem.

    What i'm stuck on is how do I set it up to where when I hover the patty over the stove and release the patty it in a way magnetizes and attaches to the stove so that the cooking can then begin?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    So this is where camera perspective and your intentions come into play. You need to define what you mean by the patty "hovering" over the stove. Unless it's a perfect top-down camera view, there will be a difference between what is "straight down" from the patty, and what is directly behind the patty from the camera's perspective. The general idea is that you will perform a raycast and see where that raycast hits and that is where you will send your patty when the user releases the mouse.

    If you want "down" to be whatever the patty is physically over, then you can do a raycast down from the patty and see where that hits, and use that position as the target for where to send the patty.

    If you want "behind my mouse according to my camera perspective" to be where the patty goes, then you can do a raycast from the camera through the mouse cursor and see what that hits, then send the patty there.

    Or maybe you have a perfectly straight down camera view and these are the same thing :D
     
  5. MirrorInstinct

    MirrorInstinct

    Joined:
    Sep 2, 2019
    Posts:
    41
    I see what you mean. Essentially, it's that I want it to be dropped to whatever the patty is physically over. Next time I get on Unity I'll have to try the raycast idea. For the sake of giving an idea as well. here are a couple screenshots of where I am at. I made some quick little placeholders to get the idea across lol Just ignore the "dining room" part to the top, just trying out some ideas for that.

    Cooking Overall Shot.png Cooking Kitchen Shot.png

    Also this is the code I've been playing with that I found online:

    Code (CSharp):
    1. {
    2.     private Vector3 mOffset;
    3.     private float mZCoord;
    4.  
    5.     Vector3 originalPos;
    6.  
    7.     void Start()
    8.     {
    9.         originalPos = transform.position;
    10.     }
    11.  
    12.     void OnMouseDown()
    13.     {
    14.         mZCoord = Camera.main.WorldToScreenPoint(
    15.         gameObject.transform.position).z;
    16.  
    17.         // Store offset = gameobject world pos - mouse world pos
    18.         mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
    19.     }
    20.  
    21.     void OnMouseUp()
    22.     {
    23.         transform.position = originalPos; //I wrote this
    24.     }
    25.  
    26.     private Vector3 GetMouseAsWorldPoint()
    27.     {
    28.         // Pixel coordinates of mouse (x,y)
    29.         Vector3 mousePoint = Input.mousePosition;
    30.  
    31.         // z coordinate of game object on screen
    32.         mousePoint.z = mZCoord;
    33.  
    34.         // Convert it to world points
    35.         return Camera.main.ScreenToWorldPoint(mousePoint);
    36.     }
    37.  
    38.     void OnMouseDrag()
    39.     {
    40.         transform.position = GetMouseAsWorldPoint() + mOffset;
    41.     }
    42. }
    Like I mentioned before though, this code only allows for the patty to be drag-able