- Home /
Question by
3kWikiGames · Dec 18, 2018 at 11:21 PM ·
movement3dtouchcamera rotatefollow
Player not following touch after camera is rotated?
So after I slant the camera slightly so in my game it seems that the touch is completely off, not following their touch and going completely random directions. This doesn't happen when it is centered rotationally at 0 and flush with the wall, but this also does not allow me to view my 3d objects the way I want, showing them as flat squares instead of a table and chair. Any way to fix this? Below ive included my code for moving and a photograph.
//Gathering resources
using UnityEngine;
using System.Collections;
//The Move class decleration
public class Move : MonoBehaviour
{
//How quickly the player follows the mouse
public float Speed = 10f;
public float maxSpeed = 50f;
public float distance = .1f;
private float moveSpeed = 10f;
public GameObject flashlight;
//Called every frame
void Update()
{
if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Stationary))
{
Speed = 10f;
moveSpeed = 10f;
maxSpeed = 50f;
}
else
{
Speed = 0;
moveSpeed = 0;
maxSpeed = 0;
}
if (Input.mousePresent)
{
movePlayer();
turnFlashlight();
}
}
private void movePlayer()
{
//Converting mouse position into Vector3
Vector3 Target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//Fixing Z-Axis
Target.z = transform.position.z;
Speed = (moveSpeed + (moveSpeed * (Vector3.Distance(Target, transform.position))));
if (Speed > maxSpeed)
{
Speed = maxSpeed;
}
if (Vector2.Distance(Target, transform.position) <= 0.1f)
{
Speed = 0;
}
//Actually move the player towards the mouse.
transform.position = Vector3.MoveTowards(transform.position, Target, Speed * Time.deltaTime / transform.localScale.x);
}
private void turnFlashlight()
{
Vector3 Target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 Click = new Vector3(Target.x, Target.y, 0.0f);
if (Input.GetMouseButton(0) & !CharacterSettings.paused)
{
flashlight.transform.LookAt(Click);
}
}
}
screenshot-240.png
(376.6 kB)
Comment