Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Abhaya.Agrawal · Aug 21, 2015 at 07:14 AM · rotationcollisionrigidbody

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);
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Aug 25, 2015 at 01:03 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges