V
V
viktorpeskov2019-10-30 13:44:14
C++ / C#
viktorpeskov, 2019-10-30 13:44:14

Why is my character not moving?

Indicated the movement by pressing the buttons. I click on the button with the mouse but the character does not respond. The keyboard is able to move it, but the control buttons - no.

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

public class Knopki : MonoBehaviour
{
    Animator anim;
  Rigidbody2D rb;
  public GameObject btnLeft;
  public GameObject btnRight;
  float PosBtnLeft;
  float PosBtnRight;
  float run;
    void Start()
    {
        PosBtnLeft = btnLeft.transform.position.y;
    PosBtnRight = btnRight.transform.position.y;
    rb = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    }

   
    void Update()
    {
        if (PosBtnLeft != btnLeft.transform.position.y){
      run = -3f;
    }
    else if (PosBtnRight != btnRight.transform.position.y){
      run = 3f;
    }
    else{
      run = 0f;
    }
    rb.velocity = new Vector2 (run, rb.velocity.x);
    }
}

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