how can I align a rotating block that falls onto another with the correct face?
Hi all,
This is my first post on Unity and my first venture into gaming so please excuse my lack of terminology or logic behind my questioning...
The premise of this particular scenario is that a rotating object is falling from the sky onto an existing, static object. Both objects have icons on each face and the aim is for the user to align the correct face with both static and falling objects to initiate the next falling object sequence.
The control is simple in concept - a single tap gesture to act as a counter force against the gravity applied to the falling object (think flappy bird logic only on a vertical, linear path or a thruster in doodle jump).
When the spinning object faces are in line with the static objects faces the user will reduce the thrust with the target of getting the two objects to collide in correct alignment. Please see attached image for example.
I have the objects, gravity and thruster working.
I'm just unsure how to execute the collision logic. My guess is that I need only define one point within a certain area and not each face but I don't know where to start. I'm also not very familiar with coding (please don't judge me too much) and have been using 'Playmaker' to get me as far as I have.
Any help would be hugely appreciated and if I've not been clear enough I'd be happy to try and explain further.
Regards,
Ash
Answer by ricardo_arango · Apr 12, 2016 at 02:39 PM
If the logic that you need is for detecting if the two objects start colliding, you can use the OnCollisionEnter callback : http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
You can also use OnCollisionStay if you want to know if the objects continue to be in contact, and for instance measure how long they have been touching : http://docs.unity3d.com/ScriptReference/Collider.OnCollisionStay.html. You would have to add a timer that would start counting when OnCollisionEnter is called, and updated in every update of OnCollisionStay.
If you want to detect the collisions much earlier (before the objects are visibly touching) you can use a trigger that is larger than the cube object, and use trigger collision events: http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html, OnTriggerStay, OnTriggerExit.
This tutorial from PlayMaker may help : https://www.youtube.com/watch?v=BYwkd80T5Zk&list=PLC759306A1E692A10∈dex=4&nohtml5=False
And this page from PlayMaker's the docs : https://hutonggames.fogbugz.com/default.asp?W30.
For specific help with PlayMaker you should try PlayMaker's forums : http://hutonggames.com/playmakerforum/index.php
If you want to know if the cubes have the same orientation, you can compare the orientation of both objects and see if they match:
transform.rotation == targetObject.transform.rotation
You would need to design your objects in way that when the rotation is equal, the symbols align.
Or you could add a trigger for each face, with a script attached to each face that exposes a variable in which you can set the name/id of the symbol of that face. When two triggers are in contact, you can compare the symbols of the faces that are in contact.
Answer by ashwhite · Apr 12, 2016 at 06:29 PM
Thanks so much for getting back to me @ricardo_arango . I think I need to be more specific with the collision points rather than a generic object collision as the side faces need to align correctly in order to initiate another falling block.
You would need to design your objects in way that when the rotation is equal, the symbols align...
Or you could add a trigger for each face, with a script attached to each face that exposes a variable in which you can set the name/id of the symbol of that face. When two triggers are in contact, you can compare the symbols of the faces that are in contact.
The design has evolved into something more visually complex however, it remains the same in game logic. The new objects will look similar to the below, including the same kind of sequence (think a low-poly object sliced up):
I'm curious as to how you would add a trigger to a face if that face is essentially part of a falling object/rigid body? I've not found a way of getting a trigger to "stick" to a rigid body yet. If you could explain further how I can achieve my game idea above I'd be very grateful.
I look forward to your response.
Thanks
Ash
That is correct, the trigger would be part of the falling object. If the two triggers are touching when the piece falling contacts the piece on the top of the stack of pieces, then you could say that the piece is correctly rotated. If the triggers are not touching each other, then it's not correctly oriented.
There isn't one single way to solve this problem by the way. This is one way that I think should be easy to set up, with prefabs, triggers and a few scripts.
Thanks for the confirmation @ricardo_arango. I had already tried this but couldn't workout how to execute the trigger that is stuck to the falling object. Can you help me with that please?
Thanks,
Ash