- Home /
Rotate Object inside room according to wall
I have a room. I am trying to move object whenever it collides with wall. its working properly except at the edge of two meeting walls. at edge its flicking. I tried to move object when its colliding at edge. but that also not working properly.
i want to use rotation like this
here's my code
using UnityEngine; using System.Collections; using DG.Tweening;
public class DragNDropObjRotation : MonoBehaviour { string lastCol; GameObject controller;
Vector3 rotation;
void Start()
{
rotation = gameObject.transform.rotation.eulerAngles;
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == AppConstants.WALL_LEFT_BACK && col.gameObject.tag != AppConstants.WALL_RIGHT_BACK && col.gameObject.tag != AppConstants.WALL_LEFT_FRONT && col.gameObject.tag != AppConstants.WALL_RIGHT_FRONT && lastCol != "" && lastCol != AppConstants.WALL_LEFT_BACK)
{
Debug.Log("inside wall left back");
MoveObjectAtCollioson(col.gameObject.tag);
lastCol = AppConstants.WALL_LEFT_BACK;
Invoke("RotateObjAfterCollision", 0.25f);
}
else if (col.gameObject.tag != AppConstants.WALL_LEFT_BACK && col.gameObject.tag == AppConstants.WALL_RIGHT_BACK && col.gameObject.tag != AppConstants.WALL_LEFT_FRONT && col.gameObject.tag != AppConstants.WALL_RIGHT_FRONT && lastCol != "" && lastCol != AppConstants.WALL_RIGHT_BACK)
{
Debug.Log("inside wall right back");
MoveObjectAtCollioson(col.gameObject.tag);
lastCol = AppConstants.WALL_RIGHT_BACK;
Invoke("RotateObjAfterCollision", 0.25f);
}
else if (col.gameObject.tag != AppConstants.WALL_LEFT_BACK && col.gameObject.tag != AppConstants.WALL_RIGHT_BACK && col.gameObject.tag == AppConstants.WALL_LEFT_FRONT && col.gameObject.tag != AppConstants.WALL_RIGHT_FRONT && lastCol != "" && lastCol != AppConstants.WALL_LEFT_FRONT)
{
Debug.Log("inside wall left front");
MoveObjectAtCollioson(col.gameObject.tag);
lastCol = AppConstants.WALL_LEFT_FRONT;
Invoke("RotateObjAfterCollision", 0.25f);
}
else if (col.gameObject.tag != AppConstants.WALL_LEFT_BACK && col.gameObject.tag != AppConstants.WALL_RIGHT_BACK && col.gameObject.tag != AppConstants.WALL_LEFT_FRONT && col.gameObject.tag == AppConstants.WALL_RIGHT_FRONT && lastCol != "" && lastCol != AppConstants.WALL_RIGHT_FRONT)
{
Debug.Log("inside wall right front");
MoveObjectAtCollioson(col.gameObject.tag);
lastCol = AppConstants.WALL_RIGHT_FRONT;
Invoke("RotateObjAfterCollision", 0.25f);
}
}
void MoveObjectAtCollioson(string currentCol)
{
Vector3 parentCol = gameObject.transform.parent.GetComponent<BoxCollider>().size;
Vector3 parentPos = gameObject.transform.parent.transform.position;
switch (currentCol)
{
case AppConstants.WALL_RIGHT_BACK:
Debug.Log("case 0: moved @ right wall back");
if (lastCol == AppConstants.WALL_LEFT_BACK)
{
Debug.Log("moving on left wall back ");
parentPos = new Vector3(parentPos.x, parentPos.y, parentPos.z - parentCol.y);
}
if (lastCol == AppConstants.WALL_LEFT_FRONT)
{
Debug.Log("moving on left wall front ");
parentPos = new Vector3(parentPos.x, parentPos.y, parentPos.z + parentCol.y);
}
break;
case AppConstants.WALL_LEFT_BACK:
Debug.Log("case 1: moved @ left wall back");
if (lastCol == AppConstants.WALL_RIGHT_BACK)
{
parentPos = new Vector3(parentPos.x - parentCol.y, parentPos.y, parentPos.z);
}
if (lastCol == AppConstants.WALL_RIGHT_FRONT)
{
parentPos = new Vector3(parentPos.x + parentCol.y, parentPos.y, parentPos.z);
}
break;
case AppConstants.WALL_LEFT_FRONT:
Debug.Log("case 2: moved @ left wall fornt");
if (lastCol == AppConstants.WALL_RIGHT_BACK)
{
parentPos = new Vector3(parentPos.x + parentCol.y, parentPos.y, parentPos.z);
}
if (lastCol == AppConstants.WALL_RIGHT_FRONT)
{
parentPos = new Vector3(parentPos.x - parentCol.y, parentPos.y, parentPos.z);
}
break;
case AppConstants.WALL_RIGHT_FRONT:
Debug.Log("case 3: moved @ right wall front");
if (lastCol == AppConstants.WALL_LEFT_BACK)
{
Debug.Log("moving on left wall back ");
parentPos = new Vector3(parentPos.x, parentPos.y, parentPos.z - parentCol.y);
}
if (lastCol == AppConstants.WALL_LEFT_FRONT)
{
Debug.Log("moving on left wall front ");
parentPos = new Vector3(parentPos.x, parentPos.y, parentPos.z + parentCol.y);
}
break;
}
}
void RotateObjAfterCollision()
{
Quaternion rotation = gameObject.transform.parent.transform.rotation;
switch (lastCol)
{
case AppConstants.WALL_RIGHT_BACK:
rotation.eulerAngles = new Vector3(0, 0, 0);
break;
case AppConstants.WALL_LEFT_BACK:
rotation.eulerAngles = new Vector3(0, 0, -90);
break;
case AppConstants.WALL_LEFT_FRONT:
rotation.eulerAngles = new Vector3(0, 0, 90);
break;
case AppConstants.WALL_RIGHT_FRONT:
rotation.eulerAngles = new Vector3(0, 0, 180);
break;
}
gameObject.transform.parent.transform.GetComponent<Rigidbody>().MoveRotation(rotation);
}
}
It looks like the example you showed uses raycast to find the normal of the wall the mouse is on. After finding that, it then finds the closest position the object can fit in that orientation and offsets it. That's my interpretation at least
Your answer
Follow this Question
Related Questions
Quirky unasked for Rotation with Ridigidbodies 0 Answers
Surface interaction between rotating RigidBodies? 0 Answers
Rigidbody goes wonky after collision 0 Answers
How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer
Rigidbody doesn't rotate after collision 3 Answers