N
N
Nike232021-06-07 01:24:09
Unity
Nike23, 2021-06-07 01:24:09

What is the proper way to write character control syntax similar to subway surfers?

I wanted to make a game where a coin falls down and it needs to maneuver during the flight and reach the coin acceptor to get a bun. The bottom line is that I do not know how to write a character control correctly. With grief in half with a couple of guides, I made a control, but there are a lot of ifs, I know that this is not correct and this should not be in the code and it is practically unreadable, but unfortunately I still don’t know how to do it, and I didn’t find much guides how to do it such management.

60bd4a0051dd3341777524.jpeg

[SerializeField] private GameObject _palyer;
   //тут я сделал 3 таргета для перемещения монетки по таргетам 
    [SerializeField] private Transform _left; 
    [SerializeField] private Transform _center;
    [SerializeField] private Transform _right;

    private int _state = 1; // Сделал int для обозначения на каком таргете находиться плеер  0-left 1-center 2-right
   // выравниваю по центру плейра в коде потому что не получаеться в unity я незаю почему всё перепробывл и пресоздовал и градус поворота проверял не получалось поставить персонажа на центр 
    private void Awake()
    {
        _palyer.transform.position = _center.transform.position;
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        if(Mathf.Abs(eventData.delta.x)> Mathf.Abs(eventData.delta.y))
        {
            if(eventData.delta.x > 0)
            {
                if (_state == 1)
                {
                    
                    _state = 2;
                    Cheker();
                }
                if (_state == 0)
                {
                    
                    _state = 1;
                    Cheker();
                }
            }
            else
            {
                if(_state == 1)
                {
                    _state = 0;
                    Cheker();
                }
                else if(_state == 2)
                {
                    _state = 1;
                    Cheker();
                }
                
            }
        }
        
    }

    public void OnDrag(PointerEventData eventData)
    {
        
    }

    private void FixedUpdate()
    {
        Cheker();
    }

    private void Cheker()
    {
        
        if (_state == 0 )
        {
            Left();

        }
        else if (_state == 1)
        {
            Center();

        }
        else if (_state == 2)
        {
            Right();
        }
    }


    private void Left()
    {

        _palyer.transform.position = Vector2.Lerp(_palyer.transform.position, _left.transform.position, (3 * Time.deltaTime));
    
    }
     private void Center()
    {
        _palyer.transform.position = Vector2.Lerp(_palyer.transform.position, _center.transform.position, (3 * Time.deltaTime));
    }

    private void Right()
    {
        _palyer.transform.position = Vector2.Lerp(_palyer.transform.position, _right.transform.position, (3 * Time.deltaTime));
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question