Hello, I am making an AR application and I managed to get some of my objects to rotate by touch, however, when I touch one object, the other object rotates at the same time. I want them to act independently of one another, where rotating one doesn’t rotate the other.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotation : MonoBehaviour
{
private float turnSpeed = 100f;
private float _startingPosition;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit Hit;
if (Physics.Raycast(ray, out Hit))
{
switch (touch.phase)
{
case TouchPhase.Began:
_startingPosition = touch.position.x;
break;
case TouchPhase.Stationary:
if (_startingPosition > touch.position.x)
{
this.transform.Rotate(Vector3.forward, turnSpeed * Time.deltaTime);
}
else if (_startingPosition < touch.position.x)
{
this.transform.Rotate(Vector3.forward, -turnSpeed * Time.deltaTime);
}
break;
case TouchPhase.Ended:
Debug.Log("Touch Phase Ended.");
break;
}
}
}
}
}