Hello, I have a camera, child of a rotated Null. This code makes the null move when mouse is on border of the screen. Script is on the Camera. My problem is that movement is always world relatives and I don't know how to make it move relatively to the null rotation (or screen coordinates?). Code (CSharp): public class RTSCamera : MonoBehaviour { public float scrollZone = 30; public float scrollSpeed = 5; public float xMax = 8; public float xMin = 0; public float zMax = 8; public float zMin = 0; private Vector3 desiredPosition; private void Start () { desiredPosition = transform.parent.position; } private void Update () { { //------------------------------x, z movement float x = 0, y = 0, z = 0; float speed = scrollZone * Time.deltaTime; if (Input.mousePosition.x < scrollZone) x -= speed; else if (Input.mousePosition.x > Screen.width - scrollZone) x += speed; if (Input.mousePosition.y < scrollZone) z -= speed; else if (Input.mousePosition.y > Screen.height - scrollZone) z += speed; Vector3 move = new Vector3 (x, y, z) + desiredPosition; move.x = Mathf.Clamp (move.x, xMin, xMax); move.z = Mathf.Clamp (move.z, zMin, zMax); desiredPosition = move; transform.parent.position = Vector3.Lerp (transform.parent.position, desiredPosition, 0.2f); } } } Thank you
That's not the only problem — you're also abusing Lerp. Anyway, there are several ways to solve this, but the easiest is probably to think of it this way: calculate the world position under the mouse cursor (by casting a ray, or doing a ray/plane intersection). Also calculate the world position several pixels "inward" (away from the edge of the screen). Find the difference between these two positions, and that's how you want to move the camera. HTH, - Joe
What I want to move is the null, parent of the camera. Basically I want to move it on its local coordinates. I am not sure that calculate world position etc. under mouse cursor is the way to do so, are you sure? I think i should deal with quaternions, but it only gave me headhache so far... Thank you for the Lerp advise, i'll take a look at it.
Yes, I've done it this way before. There are other ways to do it (though none of them involving quaternions much), but this is the most direct. You're just saying: What is this place I'm pointing at? And where would it be if it were moved this way a little bit? OK, so move it that way.
usually they're referred to as an "empty", null means nothing which is a little confusing , especially if you run into a situation where you're describing "a null reference error on the null" An "empty" is just a gameobject/transform without any other components and the terminology is taken from the text used in the creation drop down
You don't have to raycast at all. Just check if you're close to a screen edge (comparing mouse position X and Y to 0 and screen width/height) and move in that direction using the local forward and right. Code (csharp): float x, y; if (mousePos.x < 10) x = -10; else if (mousePos.x > Screen.width - 10) x = 10; if (mousePos.y < 10) y = -10; else if (mousePos.y > Screen.height - 10) y = 10; transform.position = (transform.forward * x + transform.right * y) * Time.deltaTime; If you want to do the right-click camera move then it becomes a bit more complicated because you have to store the initial mouse position and the current in order to calculate a distance delta but you still shouldn't have to do a raycast at all.
I did it this way: Code (CSharp): float x = 0, y = 0, z = 0; if (Input.mousePosition.x < scrollArea) x -= scrollSpeed * Time.deltaTime * fov; else if (Input.mousePosition.x > Screen.width - scrollArea) x += scrollSpeed * Time.deltaTime * fov; if (Input.mousePosition.y < scrollArea) z -= scrollSpeed * Time.deltaTime * fov; else if (Input.mousePosition.y > Screen.height - scrollArea) z += scrollSpeed * Time.deltaTime * fov; Vector3 move = new Vector3 (x, y, z); transform.Translate(move.x, 0, move.z, Space.Self); I multiply by fov so the far the camera, the fast the scroll (script contain a dynamic fov too).