- Home /
Scale Z Axis Movement According to Camera Tilt (Drag Control Camera)
Hello, I've been looking online for a solution for this problem but haven't been able to find one. I'm attempting to create a script to control camera movement on a mobile device, though right now I'm using the mouse as a substitution for touch in the editor. The camera is looking down on a quad at an angle, similar to the way the camera functioned in the game Age of Empires II. The drag works as expected when positioned at a 90˚ tilt in the x axis and looking straight down at the quad––a movement of one world space unit in either direction of the mouse corresponds to one world space unit of movement in the camera. When the camera is tilted, say, at 45˚I expect to scale the amount of movement of the camera in the z axis by a certain amount, at 45˚ I would expect the scale value to be the sqrt(2). However, after testing out various values through trial and error, I need to scale it by 2. At 30˚ it's 4, and at 15˚ tilt I scale the z axis movement by 15˚ relative to the z value from Camera.main.ScreenToWorldPoint(Input.mousePosition). I would like to have the ability to tilt the camera at any angle, but I have no idea how to calculate the scalar value. Is there a way to calculate it, or is there a better way to go about doing this? Thank you so much in advance for any answers, and sorry for the long-winded explanation. Oh, and the camera is in orthographic mode.
Tldr; I want to "drag" a camera around the screen looking down at a quad at an angle, but can't find the proper way to scale the mouse's z position to the camera's z position.
Code:
//this is attached to the main camera. These functions are called from a separate script that handles input.
using UnityEngine; using System.Collections;
public class CameraCtrl : MonoBehaviour {
//Methods for editor testing
public Vector3 mouseOrigin; //this serves as the initial position of the mouse to calculate the movement over one frame
//method is called when a click begins before DragEditor (Touch[] touch). The Touch[] argument goes unused in both functions.
void SingleTouchDownEditor(Touch[] touch)
{
mouseOrigin = Input.mousePosition; //set the mouse origin
}
public Vector3 dragThreshold = new Vector3(0f, 0f, 0f);
public Transform cube;
public float zValueScaler;
void DragEditor(Touch[] touch)
{
Vector3 pos;
Vector3 move;
if((Mathf.Abs(Input.mousePosition.x - mouseOrigin.x) > dragThreshold.x) || (Mathf.Abs(Input.mousePosition.y - mouseOrigin.y) > dragThreshold.y))
{
pos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.ScreenToWorldPoint(mouseOrigin);
move = new Vector3(pos.x, 0, pos.z * zValueScaler);
Debug.Log("Current MousePos: " + Camera.main.ScreenToWorldPoint(Input.mousePosition));
Debug.Log("Camera Pos: " + Camera.main.transform.position);
Camera.main.transform.Translate(-1 * move, Space.World);
}
float speed = 0.5f;
mouseOrigin = Input.mousePosition; //set mouseOrigin before the next frame
}
}
Your answer
Follow this Question
Related Questions
ScreenToWorldPoint returning weird value. 2 Answers
(solved) 2d sprite rotation towards mouse 1 Answer
World point for MousePosition 1 Answer
Cant Rotate in Unity Survival Shooting Game tutorial 0 Answers