Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

FlipX sprite while decrease/increase position value

Discussion in 'Scripting' started by freedom667, Sep 11, 2018.

  1. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    424
    I'm stuck at here. I have a character that controlling with Slider. So when i turn the slider's handle to left, FlipX of SpriteRenderer will true . when i turn to right, FlipX of SpriteRenderer will false. sure it will decrease/increase value of position when i turn left/right. So, it will not true on minus, I hope you get it. how can i do this?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I'm sorry, I'm really not sure that is clear.

    Are you saying that the image of the sprite is not exactly in the centre of its display box? Therefore when the X-axis is flipped it makes it appear as if the sprite is also moving its position? If that is the case, then just edit the image (in GiMP, PhotoShop etc.) to make it appear more central within its boundary.
     
  3. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    424
    no. I'll tell you with Slider as example. for example I have a slider. when i increase the value sprite will flip to right but when i decrease the value? then sprite will flip to left.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, I think I see what you want. So what have you tried so far?
     
  5. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    424
    I could did if position.x < 0 then flipX.enabled = true; because I don't know how to do this
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    In pseudo-code, your logic is exactly correct. Have you read up in the Unity documentation how to go about doing this?

    You will want to take the value from the slider, and set the flipX on the sprite renderer.

    So something like
    myRenderer.flipX = mySlider.value < 0;
    .
     
  7. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    424
    but I don't want it. I want if slider value is decrease flipX true, else if slider value increase flipX false.
     
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Umm, ok.

    Do you mean something like this then? (caveat: code is untested)
    Code (CSharp):
    1. public class Test : MonoBehaviour
    2. {
    3.     public void ValueChangeCheck()
    4.     {
    5.         renderer.flipX = mainSlider.value < m_previous;
    6.         m_previous = mainSlider.value;
    7.     }
    8.  
    9.     void Start()
    10.     {
    11.         mainSlider.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
    12.         m_previous = mainSlider.value;
    13.     }
    14.  
    15. #pragma warning disable 649
    16.     [SerializeField] Slider mainSlider;
    17.     [SerializeField] SpriteRenderer renderer;
    18. #pragma warning restore 649
    19.  
    20.     float m_previous;
    21. }
     
  9. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    424
    yes it works. thanks I appreciate you. can we make with rotation too? for example; at decarese it will rotate to -2, at increase it will rotate to 2.
     
  10. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You can set the transform.rotation to a value based on the condition. So after line 6 above, you could add something like
    renderer.transform.rotation = renderer.flipX ? someRotationLeft : someRotationRight;
    . You would need to supply those two rotation values, of course.
     
  11. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    424
    Thanks for help :) It works too. I tried with DOTween but did not work. Anyway, it can be like this :)
     
    Doug_B likes this.