Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 CGPHANT0M · Feb 15, 2013 at 02:39 PM · rotationtriggerscollisions

Rotating cube (World) when player reaches edge.

Hello everyone.

I am writing a script to auto-rotate a cube (the world in this game) so that when the player reaches the edge of the cube the cube rotates 90 degrees to display the next face of the cube. I have managed to do all this using 2 triggers per edge of the cube, so each face has 4 triggers. These triggers just rotate the cube by 90 degrees to position the next face, they also change the players transform.position to align with the edge of the new face, it also offsets the player upwards so he doesn't get flung into space when the cube rotates.

I Have included most of the code, all that is missing is the reaming RotateFace() functions. As they are just the same as the one included.

But basically I am just using an if statement to check for a collision by name and assign a "cameFrom" int, and then calling a particular face function (e.g FaceFive), which is a simple switch system using the "CameFrom" int.

And this works just fine for a bit, if you follow strict patterns. As when you go from face 1 to face 3 it works, but if you got to from face 1 to 3 after visiting face 5, then it doesn't work. It would seem the cube is remembering where it has been as so after a few turns the original rotation date I feed it no longer does what I want.

I thought I could get around this with the "cameFrom" int which will rotate according to the face the player came from. But this doesn't seem to work. I tried adding that useless Vecter3 at the top there to zero out the rotation on the collision event. But that doesn't help. I was up until 4 trying to get this working. Any ideas anyone? Any suggestions would be greatly appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class PlanetController : MonoBehaviour {
     
     private GameObject Slab; // This is the player
     private GameObject World;// This is the planet or world our player is on. All "Trigger" objects should be a child to the world object
     private int cameFrom;
     private Vector3 originalPos = new Vector3 (0f,0f,0f);
 
     // Use this for initialization
     void Start () 
     {    
         Slab = GameObject.Find("Slab"); // Find and assign the player object
         World = GameObject.Find("World");// Find and assign the world object
     }
     
     // Update is called once per frame
     void Update () 
     {
     }
     
     void OnTriggerEnter(Collider collider)
     {
         gameObject.transform.Rotate(originalPos); 
         if(collider.gameObject.name == ("Slab"))
         {
             if(gameObject.name == ("TriggerBlock01")) 
             {
                 cameFrom = 0;
                 RotateFaceFive();
             }
             if(gameObject.name == ("TriggerBlock02"))
             {
                 cameFrom = 2;//
                 RotateFaceOne();
             }
             if(gameObject.name == ("TriggerBlock03"))
             {
                 cameFrom = 0;//
                 RotateFaceSix();
             }
             if(gameObject.name == ("TriggerBlock04")) 
             {
                 cameFrom = 3;//
                 RotateFaceOne(); 
             }
             if(gameObject.name == ("TriggerBlock05"))
             {
                 cameFrom = 3;//
                 RotateFaceThree();
             }
             if(gameObject.name == ("TriggerBlock06")) 
             {
                 cameFrom = 1;//
                 RotateFaceSix();  
             }
             if(gameObject.name == ("TriggerBlock07"))
             {
                 cameFrom = 2;
                 RotateFaceFive();
             }
             if(gameObject.name == ("TriggerBlock08")) 
             {
                 cameFrom = 2;//
                 RotateFaceThree();
             }
             if(gameObject.name == ("TriggerBlock09"))
             {
                 cameFrom = 0; // 
                 RotateFaceTwo();
             }
             if(gameObject.name == ("TriggerBlock10"))
             {
                 cameFrom = 0;//
                 RotateFaceFour();
             }
             if(gameObject.name == ("TriggerBlock11"))
             {
                 cameFrom = 0;//
                 RotateFaceThree();
             }
             if(gameObject.name == ("TriggerBlock12"))
             {
                 cameFrom = 1;//
                 RotateFaceThree();
             }
             if(gameObject.name == ("TriggerBlock13"))
             {
                 cameFrom = 1;//
                  RotateFaceFour();
             }
             if(gameObject.name == ("TriggerBlock14"))
             {
                 cameFrom = 1;//
                 RotateFaceTwo();
             }
             if(gameObject.name == ("TriggerBlock15"))
             {
                 cameFrom = 1;//
                 RotateFaceOne(); 
             }
             if(gameObject.name == ("TriggerBlock16"))
             {
                 cameFrom = 0;//
                 RotateFaceOne();
             }
         }
     }
     public void RotateFaceOne()
     {
         switch(cameFrom)
         {
         case 0: //coming from face 1 to face 0
             World.gameObject.transform.Rotate (90,0,0);
             Slab.gameObject.transform.position = new Vector3(transform.position.x,transform.position.y + 1,transform.position.z - 1);  
             break;
         case 1://coming from face 3 to face 0
             World.gameObject.transform.Rotate (-90,0,0);
             Slab.gameObject.transform.position = new Vector3(transform.position.x,transform.position.y + 1,transform.position.z + 1);
             break;
         case 2://coming from face 4 to face 0
             World.gameObject.transform.Rotate (0,0,90);
             Slab.gameObject.transform.position = new Vector3(transform.position.x + 1,transform.position.y + 1,transform.position.z); 
             break;
         case 3://coming from face 5 to face 0
             World.gameObject.transform.Rotate (0,0,-90);
             Slab.gameObject.transform.position = new Vector3(transform.position.x - 1,transform.position.y + 1,transform.position.z); 
             break;
         }
     }
 }
Comment
Add comment · Show 4
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 CGPHANT0M · Feb 16, 2013 at 08:38 PM 0
Share

Ok, so a cheeky bump as I am still quite stuck on this one.

I have been reading some threads where people have done this sort of thing with sphere worlds, and that was done with ray casts. I just want to make this simple mechanism work, as i feel it would get too complicated using ray casts, especially with more complicated cube worlds, with stacked cubes and so on. I think the trigger event might be the best solution. But any and all ideas on how to get this to work would be a great help.

Thanks a million.

avatar image CGPHANT0M · Feb 18, 2013 at 01:39 PM 0
Share

Ok. I have been doing some tests with Raycasting, as this seems like it might be a simpler solutions, and might shed a 100 lines or so of redundant code. :)

So, I have the ray emitted from the character at a fixed distance (15 units in this case) and when it hits a particular collider in the environment it will tell the cube to rotate. But I am still having the same issues with the rotation data building up in the transform of the cube object (world). So, if you follow some strict patterns, the rotation works fine. But if you walk around randomly the cube begins to rotate counter to where it should be rotating.

Any help on how I might be able to fix this issue would be very helpful.

Thanks.

And thanks to the $$anonymous$$ods for putting up with me posting things in the wrong area, most appreciated!

avatar image robertbu · Feb 18, 2013 at 06:11 PM 0
Share

Without having the project to play with to see the issue, I'm just guessing at an answer. But I wonder if you wouldn't have better luck with an angle axis rotation (RotateAround()). As I walk from face to face, the vector that forms the axis is well defined and easy to calculate in local space.

avatar image dubbreak · Feb 18, 2013 at 11:41 PM 0
Share

Not a solution, but a suggestion: you should abstract your triggers so you can hold info with them. I.e. a little script that contains what face it is on and what the rotate too should be. That way you don't need these hardcoded if statements. You simply check the public variable of what face it's on and the expect one to go to in the trigger object and user that for your rotate logic (possibly including roberbu's suggestion as well).

A simple test project/scene would be helpful as well. Just a cube, something to represent the character, your colliders and the code you have.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detecting multiple gameobjects 1 Answer

How to make a particle system play on trigger with another box collider 1 Answer

Detect trigger touching another trigger? 1 Answer

Child Object's collider setting off triggers 1 Answer

Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 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