K
K
KeysCG2020-06-09 05:02:40
C++ / C#
KeysCG, 2020-06-09 05:02:40

How to change GetKey to UI Button?

Good day to all!
Please help me to make a code to replace the buttons on the keyboard (currently E and Q buttons) with UI buttons (for android). Tried through Event Trigger, it turned out to make only a single click, but I need the camera to rotate while holding the UI Button until you release the UI Button.
Everywhere they write what needs to be done through Event Systems, but nothing works for me :(
Please help anyone who is not difficult. I would be very grateful if someone writes a working code so that I can study and understand how it works. Many thanks to everyone!

using System;
using UnityEngine;

namespace TopDownShooter
{
    public class TopDownCamera : MonoBehaviour
    {
        public Transform Target;

        public float SeeForward;

        public float RotationSpeed = 3;

        // The speed with which the camera will be following.
        public float Smoothing = 5f;

        // The initial offset from the target.
        private Vector3 _offset;

        private bool _rotate;

        private void Start()
        {
            _offset = transform.position - Target.position;
        }

        private void Update()
        {
            //rotate camera
            if (Input.GetKey(KeyCode.E))
            {
                transform.Rotate(Vector3.up * RotationSpeed, Space.World);
            }

            if (Input.GetKey(KeyCode.Q))
            {
                transform.Rotate(Vector3.up * -RotationSpeed, Space.World);
            }
        }

        private void FixedUpdate()
        {
            var targetCamPos = Target.position + _offset + Target.forward * SeeForward;
            transform.position = Vector3.Lerp(transform.position, targetCamPos, Smoothing * 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