0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Objects : MonoBehaviour
{
    public float maxScale = 10.0f;
    public float minScale = 2.0f;
    public float shrinkSpeed = 1.0f;
    public float rotatingSpeed = 3f;
    public bool automaticScale = false;
    public bool rotateOnMouseMove = false;
    public bool rotateOnMouseDrag = true;

    private float targetScale;
    private Vector3 v3Scale;

    void Start()
    {
        v3Scale = transform.localScale;
    }

    void Update()
    {
        RaycastHit hit;
        Ray ray;

        if (Input.GetMouseButton(1))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit) && hit.transform == transform)
            {
                targetScale = minScale;
                v3Scale = new Vector3(targetScale, targetScale, targetScale);
            }

            if (automaticScale == false)
                transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime * shrinkSpeed);
        }

        if (Input.GetMouseButton(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit) && hit.transform == transform)
            {
                targetScale = maxScale;
                v3Scale = new Vector3(targetScale, targetScale, targetScale);
            }

            if (automaticScale == false)
                transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime * shrinkSpeed);
        }

        if (automaticScale == true)
        {
            transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime * shrinkSpeed);
        }

        if (rotateOnMouseMove == true)
        {
            transform.Rotate(Input.GetAxis("Mouse Y") * rotatingSpeed, -Input.GetAxis("Mouse X") * rotatingSpeed, 0, Space.World);
        }

        if (rotateOnMouseMove == true)
        {
            rotateOnMouseDrag = false;
        }

        if (rotateOnMouseDrag == true)
        {
            rotateOnMouseMove = false;
        }
    }

    private void OnMouseDrag()
    {
        if (rotateOnMouseDrag == true)
        {
            transform.Rotate(Input.GetAxis("Mouse Y") * rotatingSpeed, -Input.GetAxis("Mouse X") * rotatingSpeed, 0, Space.World);
        }
    }
}

The problem is at the bottom:

if (rotateOnMouseMove == true)
            {
                rotateOnMouseDrag = false;
            }

            if (rotateOnMouseDrag == true)
            {
                rotateOnMouseMove = false;
            }

Once I'm starting the game by default the rotateOnMouseDrag is set to true. When I click on the check box of rotateOnMoseMove it's switching between them the rotateOnMouseDrag is false and rotateOnMouseMove is true.

The problem is when then I click again on the check box of rotateOnMouseDrag but it's never switching between them again it keep staying true on the rotateOnMouseMove.

I want it to act like a switch each time one of them is true the other should be false.

\$\endgroup\$
1
\$\begingroup\$

Its a little gross to use bools in conditionals in that way, because it can become very difficult to follow the control flow. It would be easy to fix in several different ways, for example, exposing your property setters for your two bools to make sure they are always opposite (whenever you set a bool, it sets it's opposite bool too). Howwwever, I think the best thing to do here is to use an enum! It is easy to read, and it will be trivial to implement more rotation styles in the future. Also, don't forget about switch statements!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Objects : MonoBehaviour
{
    public float maxScale = 10.0f;
    public float minScale = 2.0f;
    public float shrinkSpeed = 1.0f;
    public float rotatingSpeed = 3f;
    public bool automaticScale = false;

    [System.Serializable]
    public enum RotationStyle{
        MOUSE_MOVE,
        MOUSE_DRAG
    }
    [SerializeField] private RotationStyle _rotationStyle;

    private float targetScale;
    private Vector3 v3Scale;

    void Start()
    {
        v3Scale = transform.localScale;
    }

    void Update()
    {
        RaycastHit hit;
        Ray ray;

        if (Input.GetMouseButton(1))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit) && hit.transform == transform)
            {
                targetScale = minScale;
                v3Scale = new Vector3(targetScale, targetScale, targetScale);
            }

            if (automaticScale == false)
                transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime * shrinkSpeed);
        }

        if (Input.GetMouseButton(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit) && hit.transform == transform)
            {
                targetScale = maxScale;
                v3Scale = new Vector3(targetScale, targetScale, targetScale);
            }

            if (automaticScale == false)
                transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime * shrinkSpeed);
        }

        if (automaticScale == true)
        {
            transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime * shrinkSpeed);
        }

        if (_rotationStyle == RotationStyle.MOUSE_MOVE){
            transform.Rotate(Input.GetAxis("Mouse Y") * rotatingSpeed, -Input.GetAxis("Mouse X") * rotatingSpeed, 0, Space.World);
        }

    }

    private void OnMouseDrag()
    {
        if (_rotationStyle == RotationStyle.MOUSE_DRAG)
        {
            transform.Rotate(Input.GetAxis("Mouse Y") * rotatingSpeed, -Input.GetAxis("Mouse X") * rotatingSpeed, 0, Space.World);
        }
    }

    public void SetRotationStyle(RotationStyle style){
        _rotationStyle = style;
    }
}

In this example, if you want to set the rotationStyle via script, you'd just do something like

object.SetRotationStyle(Objects.RotationStyle.MOUSE_DRAG);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.