Search Unity

Housing System: Move furniture around

Discussion in 'Scripting' started by Darkness-Seeker, Sep 26, 2017.

  1. Darkness-Seeker

    Darkness-Seeker

    Joined:
    Jun 18, 2016
    Posts:
    11
    Hi there!

    First of all, I am sorry if my english it's hard to understand, it's not my mother language.

    I am working on a Housing System where the player will be able to generate new rooms of different styles and sizes, and place different furnitures and objects around to decorate and use. The first part (all the room-oriented stuff) is complete, but when beginning the second half I have encountered a problem and I can't manage to continue:

    Right now, I am using a simple cube as a test object which has a script that:

    — Snaps the object on a "grid" by autopositioning itself only on positions with an entire value or a half exactly (0.5,1,1.5, 2 etc)
    — And a OnMouseDown function which changes a bool in order to activate a section of a FixedUpdate and move the object around to the mouse position.

    The problems or things that I can't work around are:

    — As much as I press the mouse, once the cube it's grabbed it won't just release itself.
    — I can only move the cube to the right of the left of my character, never forward or backward, as I don't get how to set the raycast to move the cube without getting off the borders of the room or moving like crazy ignoring the Grid system.

    Here's the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SnapToGrid : MonoBehaviour {
    7.  
    8.     public float grid = 0.5f;
    9.     public Color HoverColor;
    10.     private Color OriginalColor;
    11.     public bool IsGrabbed;
    12.  
    13.     float x = 0;
    14.     float z = 0;
    15.     float distance = 2;
    16.  
    17.     private void Awake()
    18.     {
    19.         OriginalColor = this.GetComponent<Renderer>().material.color;
    20.     }
    21.  
    22.     //Grid System of 0.5
    23.     void Update ()
    24.     {
    25.         if (grid > 0) {
    26.             float reciprocalGrid = 1f / grid;
    27.  
    28.             x = Mathf.Round(transform.position.x * reciprocalGrid) / reciprocalGrid;
    29.             z = Mathf.Round(transform.position.z * reciprocalGrid) / reciprocalGrid;
    30.  
    31.             transform.position = new Vector3(x, 0.5f, z);
    32.         }
    33.     }
    34.  
    35.     private void FixedUpdate()
    36.     {
    37.         if (IsGrabbed == true)
    38.         {
    39.             this.GetComponent<BoxCollider>().enabled = false;
    40.  
    41.  
    42.             Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
    43.             Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
    44.  
    45.             transform.position = objPosition;
    46.             if (Input.GetMouseButtonDown(1))
    47.             {
    48.                 IsGrabbed = false;
    49.             }
    50.         }
    51.  
    52.     }
    53.  
    54.     private void OnMouseEnter()
    55.     {
    56.         this.GetComponent<Renderer>().material.color = HoverColor;
    57.     }
    58.  
    59.     private void OnMouseExit()
    60.     {
    61.         this.GetComponent<Renderer>().material.color = OriginalColor;
    62.     }
    63.  
    64.     private void OnMouseDown()
    65.     {
    66.         IsGrabbed = !IsGrabbed;
    67.         if (IsGrabbed == false)
    68.         {
    69.             this.GetComponent<BoxCollider>().enabled = true;
    70.         }
    71.     }
    72. }
    73.  
    Here's a quick recording of the cube as it acts right now: https://i.imgur.com/VQpyY0G.gifv

    Any help or guide to follow?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I would suggest using the IPointer interfaces, instead of OnMouseXXX
    However, that's just a habit :) This would allow you to use the Drag handler, also.

    For your bool, I'd say you want to unset that in OnMouseUp rather than down.

    I'd say you can calculate your grid when it's moved only, rather than always in update.

    Maybe you could try raycasting to the floor (on its own layer or something -- up to 20 meters or whatever is comfortable as a max distance) and placing the object there?
     
  3. Darkness-Seeker

    Darkness-Seeker

    Joined:
    Jun 18, 2016
    Posts:
    11
    The thing is that I'm not looking to drag the object by maintaining down the mouse button, but to Click the object to select it, move it around with your mouse until you like where it is, and click again to set it in that place.

    About the raycasting, the problem is that I have little experience using it (a shooter and that's about it), so I don't really know where to begin.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, I see about the click to start/end the movement.
    I think the raycasting would work better, because the distance you're plugging into the screen to world point.

    I mean, give this a shot and see how it works for you? https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

    With the information you gave, I'd say you should disable the collider just once on mouse down (the first one), -- rather than re-stating the command every fixed update -- and check for mouse down in Update (rather than fixed update) to be reliable..
    :)