- Home /
Question by
unkopath · Apr 19, 2020 at 07:34 PM ·
rotationtouchtouchscreenplacementtouchphase
How to move objects with perspective camera and rotating the camera in 3D?
Hi, I'm trying to rotate my camera(50 degree in X) and also place object following the touch posiiton, here is my script, can rotate the camera and move the object but since it's perspective the object isn't moving correctly following the finger, any tips on how I can achieve that?
The goal is achieve something like in this video https://www.youtube.com/watch?v=Ljnpp5ibfGQ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotator : MonoBehaviour
{
private float initialX;
public float offset = 10f;
public float speed = 10f;
public GameObject cube;
public bool movingPlayer = false;
void Update()
{
if (Input.touchCount > 0 && !movingPlayer)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
initialX = touch.position.x;
break;
case TouchPhase.Moved:
if (touch.position.x + offset < initialX)
transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y - speed, 0f);
else if (touch.position.x - offset > initialX)
transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y + speed, 0f);
initialX = touch.position.x;
break;
}
}
if (Input.touchCount > 0 && movingPlayer)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Vector3 xx = ray.GetPoint(10f);
xx.y = 1f;
xx.x = (int)xx.x;
xx.z = (int)xx.z;
cube.transform.position = xx;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Unity 2D Touch Controll via on screen button for Mobile 0 Answers
Detect touches on ui element 1 Answer
Touch inputs Angry-bird style 0 Answers
check touch position 2 Answers
Reset touch Vector after movement 2 Answers