Hey guys,
I am making a Mahjong game for learning purposes and I have come across a hurdle. At the moment I have a script that states OnClick rotate gameObject(block), which works fine. My issue is, since this is Mahjong I only want to be able to rotate if either the left or Right side of the block is free and also no other objects are on top of the gameobject. At the moment all blocks hit by the raycast rotate, which I don’t want.
Essentially want done:
Conditions:
GameObject = block;
If RayCast hits Block && Block.leftsideHasNoObjectNextToIT || Black.RightSideHasNoObjectNextToIt && block.topsideHasNoObjectNextToIt {Block.rotateObjectCodeGoesHere}
Current Code:
public class Master_BlockRotation : MonoBehaviour
{
private Transform myTransform;
private float puzzleRotation = 180f;
private bool isRunning;
private GameObject block;
public float rotationTime;
public float rayLength;
public LayerMask layerMask;
void Update()
{
// Checks if mouse clicks on an object
// If true, check if raycast hits object of layer 9/block and rotate object
if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject() && !isRunning)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.gameObject.layer == 9)
{
block = hit.collider.gameObject;
myTransform = block.GetComponent<Transform>();
StartCoroutine(CoroutineTest(this.rotationTime)); //You can start coroutines like this too, which is faster and safer
}
}
}
IEnumerator CoroutineTest(float rotationTime)
{
isRunning = true;
//Store these here. Now we can use Slerp as designed
Quaternion initialRot = myTransform.rotation;
Quaternion finalRot = Quaternion.Euler(0.0f, 0.0f, puzzleRotation);
//i is our progress through the rotation, 0 = 0%, 1 = 100%
//The increment ensures it will take 'rotationTime' seconds to reach 100%
for (float i = 0; i < 1.0f; i += Time.deltaTime / rotationTime)
{
myTransform.rotation = Quaternion.Slerp(initialRot, finalRot, i);
yield return null; //Wait one frame, then continue
}
//Make sure we're definitely at the final rotation.
//The for loop may complete just before the final rotation
myTransform.rotation = finalRot;
isRunning = false;
}
}