Y
Y
yraiv2020-05-09 17:21:30
Unity
yraiv, 2020-05-09 17:21:30

How to make smooth movement through interactive buttons on android in unity3d?

I made buttons and hung scripts on them, but when I press the button, the machine only twitches once, how to execute the script while the button is pressed?

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

public class CarControl : MonoBehaviour
{
    public GameObject Car;
    public float speed;
   
    public void Start()
    {
        Car.GetComponent<Rigidbody2D>();
    }

   
    public void MoveRightDown()
    {
        transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
        Debug.Log("нажатие на кнопку вправо");
        
    }
    public void MoveRightUp()
    {      
        Debug.Log("нажатие на кнопку вправо");
    }

    public void MoveLeftUp()
    {
        transform.position -= new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
        Debug.Log("нажатие на кнопку влево");
    }


I tried to call MoveDownRightDown through the update selected event triger, and then stop, but it seems that I'm doing it wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
2
2CHEVSKII, 2020-05-09
@yraiv

I would do this - when you click on the right button - speed = 100 (for example), on the left - speed = -100 .
When the button is released - speed = 0 .
In Update/FixedUpdate move the car using the speed value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question