Hello guys, i’m developing an fps game on smartphone and got stuck on the commands. Basically what i wanted to do was to use the left area of the smartphone’s screen just to look around(so camera movements), so i created a canvas as a touch area(named “ta” in the script) but it doesn’t work. Without declaring the touch inputs inside the if [if (ta.GetComponent().Contains(t.position))] camera commands works just fine but i’d like it to to work only in the left half of the screen( maybe in a canvas/panel).
Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Mov : MonoBehaviour
{
public Joystick joystickCamera;
public GameObject player;
public Camera thisC;
private Touch we = new Touch();
public float speed = 0.1f, moveSpeed;
public bool pressed = false;
Transform cameraTransform;
public GameObject ta;
private float rotX =0f;
private float rotY =0f;
public GameObject testo;
// Start is called before the first frame update
void Start()
{
testo = GameObject.FindGameObjectWithTag("t");
ta = GameObject.FindGameObjectWithTag("bU");
cameraTransform = Camera.main.transform;
joystickCamera = Joystick.FindObjectOfType<Joystick>();
player = GameObject.FindGameObjectWithTag("Player");
testo.GetComponent<Text>().text = "ASD";
//Screen.lockCursor = true;
//cameraTransform.eulerAngles = new Vector3(Mathf.Clamp(cameraTransform.eulerAngles.x, -90, 90), 0, 0);
}
/// </summary>
// Update is called once per frame
void Update()
{
foreach (Touch t in Input.touches) {
if (ta.GetComponent<Rect>().Contains(t.position)) {
testo.GetComponent<Text>().text = "HELP";
//Debug.Log("fff");
//if () {
if (t.phase == TouchPhase.Began)
{
we.position = t.position;
}
if (t.phase == TouchPhase.Moved)
{
float deltaX = we.position.x - t.position.x;
float deltaY = we.position.y - t.position.y;
rotX -= deltaX * Time.deltaTime * speed;
rotY += deltaY * Time.deltaTime * speed;
//this.transform.eulerAngles = new Vector3(rotY, rotX, 0f);
thisC.transform.eulerAngles = new Vector3(rotY, rotX, 0f);
//player.transform.Rotate(0, thisC.transform.rotation.y, 0);
player.transform.eulerAngles = new Vector3(0f, rotX, 0f);
}
}
}
/*player.transform.Rotate(0, Input.GetAxis("Mouse X") * speed * Time.fixedDeltaTime,0);
this.transform.Rotate(-Input.GetAxis("Mouse Y") * speed * Time.fixedDeltaTime, 0,0);*/
if (pressed)
{
player.transform.Translate(Vector3.forward * moveSpeed);
}
}
public void onPointerDownRaceButton(UnityEngine.EventSystems.BaseEventData eventData)
{
pressed = true;
}
public void onPointerUpRaceButton(UnityEngine.EventSystems.BaseEventData eventData)
{
pressed = false;
}
}
Any help is really appreciated.