Search Unity

Scaling an object using a 2 axis controller

Discussion in 'Scripting' started by daniel_johnston, Mar 12, 2020.

  1. daniel_johnston

    daniel_johnston

    Joined:
    Jul 25, 2017
    Posts:
    2
    Hi, I am trying to scale an object based on the input of an oculus touch thumbstick. It does work...kind of. However, when the stick goes back to the centre, the scale value will go back to its original value. I have scaled the axis values to go from -1/1 to 0/1. I cant for the life of me figure this out, probably something super simple I am missing. Any help would be great!!

    Thanks,

    Dan

    private void ScaleObject(Vector 2 axis)
    {

    // Scale the values between 0 - 1
    xScale = ScaleValue(-1f,1f,0f,1f,axis.x);
    yScale = ScaleValue(-1f,1f,0f,1f,axis.y);

    // Scale the object based on new values
    this.transform.localScale = new Vector3(xScale, yScale, 0.1f);

    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Please use code tags. See the first post in the forum.

    Your UI code above is stateless: it always takes input and applies scale.

    What you will need is something that is stateful, ie. changes into a state that accepts scale, and changes back out of it when you have finished setting the scale. How you toggle between those modes could be as simple as holding down a trigger, or holding a key, or tapping a key, or pointing in a direction, or whatver you want.

    But you need to detect that condition, change the state, and modify the code to behave differently when in the state.

    This construct is very broadly referred to as a "state machine." I am suggesting an extremely simple state machine above, one with only two states.