1

I'm trying to make an Android FPS Game in Unity; It was good until I get to the Looking Input code. Here what I tried:

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

public class LookInput : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public Vector2 lookInput;
    public float smoothTime = 8f;
    public float sensivity = 3f;

    Vector2 lastPosition;

    int touchIndex = 0;

    // Start is called before the first frame update
    void Start()
    {
        touchIndex = -1;
    }

    // Update is called once per frame
    void Update()
    {
        if (touchIndex != -1)
        {
            lastPosition = Vector2.Lerp(lastPosition, Input.touches[touchIndex].position, smoothTime * Time.fixedDeltaTime);

            lookInput = (Input.touches[touchIndex].position - lastPosition) / 10 * sensivity;

            GameInput.lookInput = lookInput;
        }
    }

    public void OnPointerDown(PointerEventData data)
    {
        touchIndex = data.pointerId;
    }

    public void OnPointerUp(PointerEventData data)
    {
        touchIndex = -1;
    }
}

But it didn't work.

I Also tried

override

for OnPointerDown & OnPointerUp. But it gave me some Errors:

Assets\LookInput.cs(36,26): error CS0115: 'LookInput.OnPointerDown(PointerEventData)': no suitable method found to override Assets\LookInput.cs(40,26): error CS0115: 'LookInput.OnPointerUp(PointerEventData)': no suitable method found to override

Thanks. (Sorry if English is bad)

Hadi89
  • 11
  • 2
  • Is that related to Target Platform? Because It works fine in android. – Hadi89 Aug 15 '23 at 06:36
  • What error did you get before you tried the override? – Voidsay Aug 15 '23 at 07:08
  • odd, having removed your start and update which arent relevant to this, i pasted your code into my project, and it compiled and ran.. – BugFinder Aug 15 '23 at 07:45
  • No need/use for `override` here .. no parent class is implementing those ... what exactly didn't work with the code the way you show it here? – derHugo Aug 15 '23 at 16:11
  • Btw in general `data.pointerId` is **NOT** an index within `Input.touches`!! See [`Touch.fingerId`](https://docs.unity3d.com/ScriptReference/Touch-fingerId.html). You rather get your according touch via e.g. `Input.touches.First(t => t.fingerId == touchIndex);` – derHugo Aug 15 '23 at 16:18

0 Answers0