Friday, November 4, 2022
HomeGame Developmentunity - Getting the face of a dice when utilizing arrows to...

unity – Getting the face of a dice when utilizing arrows to rotate it


I’ve obtained a 3D dice in a scene, and rotatable utilizing arrow keys. That is what it appears to be like like, think about that the packing containers are left, proper, up, and down.

enter image description here

In contrast to different questions, I’m not casting a ray forged on the dice instantly, although I’m attempting to do that to determine what face is dealing with the digicam.

I would like to have the ability to determine what face is dealing with the digicam, and when the arrows are what set off the motion 90′ in a given course.

As soon as I pay attention for the arrows shifting left or proper, I attempt to forged a Raycast instantly on the dice – however it does not appear to hit something or make a distinction if I exploit a 2D or 3D raycast. My scene is in 2D, however my dice is 3D. What am I doing improper?

Right here is my code:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public enum MCFace
{
    None,
    Up,
    Down,
    East,
    West,
    North,
    South
}

public class CubeHandling : MonoBehaviour
{
    personal void OnEnable()
    {
        TouchScreenManager.SignalButton += CheckCubeFace;
    }

    personal void OnDisable()
    {
        TouchScreenManager.SignalButton -= CheckCubeFace;
    }

    void CheckCubeFace(GameObject arrow)
    {
        if (arrow.identify == "Left" || arrow.identify == "Proper")
        {
            // now forged one other raycast on the dice
            RaycastHit hit;
            Ray ray = Digital camera.major.ScreenPointToRay(gameObject.rework.place);
            RaycastHit2D hit2D = Physics2D.GetRayIntersection(ray);

            Debug.Log("the ray is: " + ray + " and the 2nd hit is " + hit2D);

            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log("the ray is: " + ray + " and the hit is  " + hit.regular + " and the 2nd hit is " + hit2D);
            }

            Debug.Log("it is working however not taking place");
        }

            MCFace face = GetHitFace(hit);
    }

    public MCFace GetHitFace(RaycastHit hit)
    {
        Vector3 incomingVec = hit.regular - Vector3.up;

        if (incomingVec == new Vector3(0, -1, -1))
            return MCFace.South;

        if (incomingVec == new Vector3(0, -1, 1))
            return MCFace.North;

        if (incomingVec == new Vector3(0, 0, 0))
            return MCFace.Up;

        if (incomingVec == new Vector3(1, 1, 1))
            return MCFace.Down;

        if (incomingVec == new Vector3(-1, -1, 0))
            return MCFace.West;

        if (incomingVec == new Vector3(1, -1, 0))
            return MCFace.East;

        return MCFace.None;
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments