Bound Dragging of Objects inside box to box












0















Room containing sphere and cube



Hi,



As a part of my project, I have a sphere and a cube within a box. The cube is dragged by the user through the use of a mouse. The sphere moves to random coordinates within the box. For a long time, I have struggled to figure out how to bound both objects to the box, which consists of 6 rectangular prisms (one for each wall + roof + floor).



Could all of you please help me figure out how to do this?



The code used to control the sphere and the cube is posted below.



Sphere



using System.Collections;
using System.IO;

using UnityEngine;

public class PlayerController : MonoBehaviour
{
private float movementDuration = 2.0f;
private WaitForSeconds waitBeforeMoving = new WaitForSeconds(2f);
private Vector3 path = new Vector3[20];
private string filepath = null;

private void Start()
{
StartCoroutine(MainRoutine());
filepath = Application.dataPath + "/Player.txt";
File.WriteAllText(filepath, "The player blob visited these random coordinates: nn");
}

private IEnumerator MainRoutine()
{
//generate new path:
for (int i = 0; i < path.Length; i++)
{
float timer = 0.0f;
Vector3 startPos = transform.position;
float x = RandomNum(timer);
float y = RandomNum(x);
float z = RandomNum(y);
path[i] = new Vector3(x, y, z);
}

//traverse path:
for (int i = 0; i < path.Length; i++)
{
float timer = 0.0f;
Vector3 startPos = transform.position;
while (timer < movementDuration)
{
timer += Time.deltaTime;
float t = timer / movementDuration;
//t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.position = Vector3.Lerp(startPos, path[i], t);
yield return null;
}
yield return waitBeforeMoving;
}

//print path:
PrintPoints();
}

private void PrintPoints()
{

foreach (Vector3 vector in path)
{
File.AppendAllText(filepath, string.Format("{0}nn", JsonUtility.ToJson(vector)));
}
}

private float RandomNum(float lastRandNum)
{
//Random value range can be changed in the future if necessary
float randNum = Random.Range(-30.0f, 30.0f);
return System.Math.Abs(randNum - lastRandNum) < float.Epsilon ? RandomNum(randNum) : randNum;
}

}


Cube



using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

[RequireComponent(typeof(MeshCollider))]


public class UserController : MonoBehaviour
{
private Vector3 Dist;
private float PosX = 0.0f;
private float PosY = 0.0f;
private float PosZ = 0.0f;
private bool shiftOn = false;

private void OnMouseDown()
{

Dist = Camera.main.WorldToScreenPoint(transform.position);
PosX = Input.mousePosition.x - Dist.x;
PosY = Input.mousePosition.y - Dist.y;
PosZ = Input.mousePosition.z - Dist.z;

}
private void OnMouseDrag()
{

if (Input.GetMouseButton(0))
{
if (shiftOn)
{
//3D Drag, courtesy of Unity Forums
//float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
}

else
{
//Plane Drag
//float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
}



}

}



// Update is called once per frame
private void Update()
{

if (Input.GetKeyDown(KeyCode.LeftShift)||Input.GetKeyDown(KeyCode.RightShift))
{
Debug.Log("Shift Pressed"); //Logs message to the Unity Console.
shiftOn = true;
}

if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
{
Debug.Log("Shift Released"); //Logs message to the Unity Console.
shiftOn = false;
}



}



}









share|improve this question



























    0















    Room containing sphere and cube



    Hi,



    As a part of my project, I have a sphere and a cube within a box. The cube is dragged by the user through the use of a mouse. The sphere moves to random coordinates within the box. For a long time, I have struggled to figure out how to bound both objects to the box, which consists of 6 rectangular prisms (one for each wall + roof + floor).



    Could all of you please help me figure out how to do this?



    The code used to control the sphere and the cube is posted below.



    Sphere



    using System.Collections;
    using System.IO;

    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {
    private float movementDuration = 2.0f;
    private WaitForSeconds waitBeforeMoving = new WaitForSeconds(2f);
    private Vector3 path = new Vector3[20];
    private string filepath = null;

    private void Start()
    {
    StartCoroutine(MainRoutine());
    filepath = Application.dataPath + "/Player.txt";
    File.WriteAllText(filepath, "The player blob visited these random coordinates: nn");
    }

    private IEnumerator MainRoutine()
    {
    //generate new path:
    for (int i = 0; i < path.Length; i++)
    {
    float timer = 0.0f;
    Vector3 startPos = transform.position;
    float x = RandomNum(timer);
    float y = RandomNum(x);
    float z = RandomNum(y);
    path[i] = new Vector3(x, y, z);
    }

    //traverse path:
    for (int i = 0; i < path.Length; i++)
    {
    float timer = 0.0f;
    Vector3 startPos = transform.position;
    while (timer < movementDuration)
    {
    timer += Time.deltaTime;
    float t = timer / movementDuration;
    //t = t * t * t * (t * (6f * t - 15f) + 10f);
    transform.position = Vector3.Lerp(startPos, path[i], t);
    yield return null;
    }
    yield return waitBeforeMoving;
    }

    //print path:
    PrintPoints();
    }

    private void PrintPoints()
    {

    foreach (Vector3 vector in path)
    {
    File.AppendAllText(filepath, string.Format("{0}nn", JsonUtility.ToJson(vector)));
    }
    }

    private float RandomNum(float lastRandNum)
    {
    //Random value range can be changed in the future if necessary
    float randNum = Random.Range(-30.0f, 30.0f);
    return System.Math.Abs(randNum - lastRandNum) < float.Epsilon ? RandomNum(randNum) : randNum;
    }

    }


    Cube



    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;

    [RequireComponent(typeof(MeshCollider))]


    public class UserController : MonoBehaviour
    {
    private Vector3 Dist;
    private float PosX = 0.0f;
    private float PosY = 0.0f;
    private float PosZ = 0.0f;
    private bool shiftOn = false;

    private void OnMouseDown()
    {

    Dist = Camera.main.WorldToScreenPoint(transform.position);
    PosX = Input.mousePosition.x - Dist.x;
    PosY = Input.mousePosition.y - Dist.y;
    PosZ = Input.mousePosition.z - Dist.z;

    }
    private void OnMouseDrag()
    {

    if (Input.GetMouseButton(0))
    {
    if (shiftOn)
    {
    //3D Drag, courtesy of Unity Forums
    //float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
    transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
    }

    else
    {
    //Plane Drag
    //float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
    Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
    transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
    }



    }

    }



    // Update is called once per frame
    private void Update()
    {

    if (Input.GetKeyDown(KeyCode.LeftShift)||Input.GetKeyDown(KeyCode.RightShift))
    {
    Debug.Log("Shift Pressed"); //Logs message to the Unity Console.
    shiftOn = true;
    }

    if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
    {
    Debug.Log("Shift Released"); //Logs message to the Unity Console.
    shiftOn = false;
    }



    }



    }









    share|improve this question

























      0












      0








      0








      Room containing sphere and cube



      Hi,



      As a part of my project, I have a sphere and a cube within a box. The cube is dragged by the user through the use of a mouse. The sphere moves to random coordinates within the box. For a long time, I have struggled to figure out how to bound both objects to the box, which consists of 6 rectangular prisms (one for each wall + roof + floor).



      Could all of you please help me figure out how to do this?



      The code used to control the sphere and the cube is posted below.



      Sphere



      using System.Collections;
      using System.IO;

      using UnityEngine;

      public class PlayerController : MonoBehaviour
      {
      private float movementDuration = 2.0f;
      private WaitForSeconds waitBeforeMoving = new WaitForSeconds(2f);
      private Vector3 path = new Vector3[20];
      private string filepath = null;

      private void Start()
      {
      StartCoroutine(MainRoutine());
      filepath = Application.dataPath + "/Player.txt";
      File.WriteAllText(filepath, "The player blob visited these random coordinates: nn");
      }

      private IEnumerator MainRoutine()
      {
      //generate new path:
      for (int i = 0; i < path.Length; i++)
      {
      float timer = 0.0f;
      Vector3 startPos = transform.position;
      float x = RandomNum(timer);
      float y = RandomNum(x);
      float z = RandomNum(y);
      path[i] = new Vector3(x, y, z);
      }

      //traverse path:
      for (int i = 0; i < path.Length; i++)
      {
      float timer = 0.0f;
      Vector3 startPos = transform.position;
      while (timer < movementDuration)
      {
      timer += Time.deltaTime;
      float t = timer / movementDuration;
      //t = t * t * t * (t * (6f * t - 15f) + 10f);
      transform.position = Vector3.Lerp(startPos, path[i], t);
      yield return null;
      }
      yield return waitBeforeMoving;
      }

      //print path:
      PrintPoints();
      }

      private void PrintPoints()
      {

      foreach (Vector3 vector in path)
      {
      File.AppendAllText(filepath, string.Format("{0}nn", JsonUtility.ToJson(vector)));
      }
      }

      private float RandomNum(float lastRandNum)
      {
      //Random value range can be changed in the future if necessary
      float randNum = Random.Range(-30.0f, 30.0f);
      return System.Math.Abs(randNum - lastRandNum) < float.Epsilon ? RandomNum(randNum) : randNum;
      }

      }


      Cube



      using System.Collections;
      using System.Collections.Generic;
      using System.IO;
      using UnityEngine;

      [RequireComponent(typeof(MeshCollider))]


      public class UserController : MonoBehaviour
      {
      private Vector3 Dist;
      private float PosX = 0.0f;
      private float PosY = 0.0f;
      private float PosZ = 0.0f;
      private bool shiftOn = false;

      private void OnMouseDown()
      {

      Dist = Camera.main.WorldToScreenPoint(transform.position);
      PosX = Input.mousePosition.x - Dist.x;
      PosY = Input.mousePosition.y - Dist.y;
      PosZ = Input.mousePosition.z - Dist.z;

      }
      private void OnMouseDrag()
      {

      if (Input.GetMouseButton(0))
      {
      if (shiftOn)
      {
      //3D Drag, courtesy of Unity Forums
      //float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
      transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
      }

      else
      {
      //Plane Drag
      //float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
      Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
      transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
      }



      }

      }



      // Update is called once per frame
      private void Update()
      {

      if (Input.GetKeyDown(KeyCode.LeftShift)||Input.GetKeyDown(KeyCode.RightShift))
      {
      Debug.Log("Shift Pressed"); //Logs message to the Unity Console.
      shiftOn = true;
      }

      if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
      {
      Debug.Log("Shift Released"); //Logs message to the Unity Console.
      shiftOn = false;
      }



      }



      }









      share|improve this question














      Room containing sphere and cube



      Hi,



      As a part of my project, I have a sphere and a cube within a box. The cube is dragged by the user through the use of a mouse. The sphere moves to random coordinates within the box. For a long time, I have struggled to figure out how to bound both objects to the box, which consists of 6 rectangular prisms (one for each wall + roof + floor).



      Could all of you please help me figure out how to do this?



      The code used to control the sphere and the cube is posted below.



      Sphere



      using System.Collections;
      using System.IO;

      using UnityEngine;

      public class PlayerController : MonoBehaviour
      {
      private float movementDuration = 2.0f;
      private WaitForSeconds waitBeforeMoving = new WaitForSeconds(2f);
      private Vector3 path = new Vector3[20];
      private string filepath = null;

      private void Start()
      {
      StartCoroutine(MainRoutine());
      filepath = Application.dataPath + "/Player.txt";
      File.WriteAllText(filepath, "The player blob visited these random coordinates: nn");
      }

      private IEnumerator MainRoutine()
      {
      //generate new path:
      for (int i = 0; i < path.Length; i++)
      {
      float timer = 0.0f;
      Vector3 startPos = transform.position;
      float x = RandomNum(timer);
      float y = RandomNum(x);
      float z = RandomNum(y);
      path[i] = new Vector3(x, y, z);
      }

      //traverse path:
      for (int i = 0; i < path.Length; i++)
      {
      float timer = 0.0f;
      Vector3 startPos = transform.position;
      while (timer < movementDuration)
      {
      timer += Time.deltaTime;
      float t = timer / movementDuration;
      //t = t * t * t * (t * (6f * t - 15f) + 10f);
      transform.position = Vector3.Lerp(startPos, path[i], t);
      yield return null;
      }
      yield return waitBeforeMoving;
      }

      //print path:
      PrintPoints();
      }

      private void PrintPoints()
      {

      foreach (Vector3 vector in path)
      {
      File.AppendAllText(filepath, string.Format("{0}nn", JsonUtility.ToJson(vector)));
      }
      }

      private float RandomNum(float lastRandNum)
      {
      //Random value range can be changed in the future if necessary
      float randNum = Random.Range(-30.0f, 30.0f);
      return System.Math.Abs(randNum - lastRandNum) < float.Epsilon ? RandomNum(randNum) : randNum;
      }

      }


      Cube



      using System.Collections;
      using System.Collections.Generic;
      using System.IO;
      using UnityEngine;

      [RequireComponent(typeof(MeshCollider))]


      public class UserController : MonoBehaviour
      {
      private Vector3 Dist;
      private float PosX = 0.0f;
      private float PosY = 0.0f;
      private float PosZ = 0.0f;
      private bool shiftOn = false;

      private void OnMouseDown()
      {

      Dist = Camera.main.WorldToScreenPoint(transform.position);
      PosX = Input.mousePosition.x - Dist.x;
      PosY = Input.mousePosition.y - Dist.y;
      PosZ = Input.mousePosition.z - Dist.z;

      }
      private void OnMouseDrag()
      {

      if (Input.GetMouseButton(0))
      {
      if (shiftOn)
      {
      //3D Drag, courtesy of Unity Forums
      //float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
      transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
      }

      else
      {
      //Plane Drag
      //float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
      Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
      transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
      }



      }

      }



      // Update is called once per frame
      private void Update()
      {

      if (Input.GetKeyDown(KeyCode.LeftShift)||Input.GetKeyDown(KeyCode.RightShift))
      {
      Debug.Log("Shift Pressed"); //Logs message to the Unity Console.
      shiftOn = true;
      }

      if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
      {
      Debug.Log("Shift Released"); //Logs message to the Unity Console.
      shiftOn = false;
      }



      }



      }






      c# unity3d graphics monodevelop bounds






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 29 '18 at 8:53









      I DebarshiI Debarshi

      13




      13
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968109%2fbound-dragging-of-objects-inside-box-to-box%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968109%2fbound-dragging-of-objects-inside-box-to-box%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas