Edgar
Edgar

Reputation: 41

How to rotate the cube I click instead of all cubes rotating at the same time?

So i am instantiating cubes and i want to rotate a single cube which i click, however when i click on the cube, all instantiated cubes rotate at the same time, I keep trying with boolean but no success. Any help would be appreciated. UNITY 2D

{
    public int rotationDirection = -1; //-1 for clockwise
    public int rotationStep = 5; //Should be less than 90

    public bool noRotation;

    // public GameObject cubeBox;
    private Vector3 currentRotation, targetRotation;

    // Update is called once per frame
    void Update()
    {
       
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

            if (hit.collider != null && hit.collider.tag == "colourBox" && noRotation == false)
            {
                Debug.Log("object clicked: " + hit.collider.tag);
                
                RotateCube(); 
            }
        }
    }
    void RotateCube()
    {
        currentRotation = gameObject.transform.eulerAngles;
        targetRotation.z = (currentRotation.z + (90 * rotationDirection));
        StartCoroutine(objectRotationAnimation());
    }

    IEnumerator objectRotationAnimation()
    {
        currentRotation.z += (rotationStep * rotationDirection);
        gameObject.transform.eulerAngles = currentRotation;

        yield return new WaitForSeconds(0);

        if (((int)currentRotation.z > (int)targetRotation.z && rotationDirection < 0))
        {
            StartCoroutine(objectRotationAnimation());
        }
        
    }
}

Upvotes: 2

Views: 131

Answers (2)

Daniel M
Daniel M

Reputation: 766

If I understand you correctly this script is present on all cubes.

The problem is that when you click with your mouse every single cube will create its own instance of a raycast. After that every cube will check if the hit.collider is not null, if its tag is colourBox and if noRotation equals false.

You in fact don't check anywhere what cube you just clicked on. You can simply add this check by comparing the instance ID of the gameobject the raycast collided with and the instance ID of the gameobject where the script runs.

if (hit.collider != null && hit.collider.tag == "colourBox" && noRotation == false)
{
    // Check if the cube that was clicked was this cube
    if (hit.collider.gameObject.GetInstanceID() == gameObject.GetInstanceID())
    {
        Debug.Log("object clicked: " + hit.collider.tag);

        RotateCube();
    }
}

This way every cube will compare its instance ID with the instance ID of the clicked object. Only the clicked object will return true in this if statement and thus runs the RotateCube method.

Upvotes: 2

Edgar
Edgar

Reputation: 41

Used this instead of RayCastHit2D ....Deleted everything inside Update and created a new void.

private void OnMouseDown()
    {
        RotateCube();
    }

Upvotes: 1

Related Questions