- Home /
RTS Camera Script: Mapbounds and Camerascroll (C#)
I have a few problems getting my script working:
At first the camerascroll (the part with the GUI) worked well but now I only can use my mouse to scroll in 2 directions (down and left).
When my camera moves to the top or right terrainborder it jumps to the top right corner and when it approaches to the left or to the down edge it jumps to the down left corner.
using UnityEngine; using System.Collections; public class CamMove : MonoBehaviour { public float CamSpeed = 0.50f; public float CamSpeedShift = 1.00f; public float GUIsize = 10.50f; public int maxCameraHeight = 100; public int minCameraHeight = 15; public float cameraHeight; public float cameraY; public Quaternion CameraAngleNormal; public Quaternion CameraAngleSpace; private bool Angle = false; public Vector3 MapMaxBounds; public Vector3 MapMinBounds; public float turnSpeed = 4.0f; public Terrain Map; private GameObject ScrollAngle; public GameObject MainCamera; // Use this for initialization void Start () { float bounds = 0.00f; var MapTransform = Map.transform; MapMinBounds = new Vector3(MapTransform.position.x, 0, MapTransform.position.z); MapMaxBounds += MapMinBounds + new Vector3(Map.terrainData.size.x, 0, Map.terrainData.size.z); MapMinBounds.x += bounds; MapMinBounds.z += bounds; MapMaxBounds.x -= bounds; MapMaxBounds.z -= bounds; ScrollAngle = new GameObject (); transform.localRotation = CameraAngleNormal; } // Update is called once per frame void Update () { if(transform.position.x > MapMaxBounds.x) { transform.position = new Vector3 (MapMaxBounds.x, transform.position.y, transform.position.x); } if(transform.position.z > MapMaxBounds.z) { transform.position = new Vector3 (MapMaxBounds.z, transform.position.y, transform.position.z); } if(transform.position.x < MapMinBounds.x) { transform.position = new Vector3 (MapMinBounds.x, transform.position.y, transform.position.x); } if(transform.position.z < MapMinBounds.z) { transform.position = new Vector3 (MapMinBounds.z, transform.position.y, transform.position.z); } } void RTSCamMove () { Rect recdown = new Rect (0, 0, Screen.width, GUIsize); Rect recup = new Rect (0, Screen.height-GUIsize, Screen.width, GUIsize); Rect recleft = new Rect (0, 0, GUIsize, Screen.height); Rect recright = new Rect (Screen.width-GUIsize, 0, GUIsize, Screen.height); if (Input.GetKey(KeyCode.S) | recdown.Contains(Input.mousePosition)) { transform.Translate(0, 0, -CamSpeed, Space.World); } if (Input.GetKey(KeyCode.W) | recup.Contains(Input.mousePosition)) { transform.Translate(0, 0, CamSpeed, Space.World); } if (Input.GetKey(KeyCode.A) | recleft.Contains(Input.mousePosition)) { transform.Translate( -CamSpeed, 0, 0, Space.World); } if (Input.GetKey(KeyCode.D) | recright.Contains(Input.mousePosition)) { transform.Translate(CamSpeed, 0, 0, Space.World); } // Shift if (Input.GetKey(KeyCode.S) & Input.GetKey(KeyCode.LeftShift)) { transform.Translate(0, 0, -CamSpeed, Space.World); } if (Input.GetKey(KeyCode.W) & Input.GetKey(KeyCode.LeftShift)) { transform.Translate(0, 0, CamSpeed, Space.World); } if (Input.GetKey(KeyCode.A) & Input.GetKey(KeyCode.LeftShift)) { transform.Translate( -CamSpeed, 0, 0, Space.World); } if (Input.GetKey(KeyCode.D) & Input.GetKey(KeyCode.LeftShift)) { transform.Translate(CamSpeed, 0, 0, Space.World); } if (Input.GetKeyDown(KeyCode.Space)) { Angle = !Angle; } if (Angle == true) { transform.localRotation = CameraAngleSpace; } if (Angle == false) { transform.localRotation = CameraAngleNormal; } } void LateUpdate () { RTSCamScroll (); } void RTSCamScroll () { float deadZone = 0.01f; float ZoomFactor = 20.00f; float ScrollWheelValue = Input.GetAxis("Mouse ScrollWheel") * ZoomFactor; if((ScrollWheelValue > -deadZone && ScrollWheelValue < deadZone) || ScrollWheelValue == 0) return; var EularAnglesX = MainCamera.transform.localEulerAngles.x; ScrollAngle.transform.position = transform.position; ScrollAngle.transform.eulerAngles = new Vector3(EularAnglesX, this.transform.eulerAngles.y); ScrollAngle.transform.Translate(Vector3.forward * ScrollWheelValue); Vector3 desiredScrollPosition = ScrollAngle.transform.position; if(desiredScrollPosition.y > maxCameraHeight) return; if(desiredScrollPosition.y < minCameraHeight) return; float heightDifference = desiredScrollPosition.y - this.transform.position.y; cameraHeight = heightDifference; cameraY = desiredScrollPosition.y; this.transform.position = desiredScrollPosition; return; } }
Answer by Zodiarc · Jun 16, 2014 at 06:47 AM
Try here http://answers.unity3d.com/questions/720383/fixing-issues-with-custom-rts-camera-controll-scri.html
Should be a good start.
Your answer
Follow this Question
Related Questions
Pan RTS camera controller without changing elevation/zoom 0 Answers
Distribute terrain in zones 3 Answers
Error about Camera panning. 0 Answers
RTS CAMERA C# 2 Answers
Multiple Cars not working 1 Answer