Good day, everyone!
I’m working on a game that requires the player’s camera movement to be limited to certain restrictions based on ship size. I have the following code, but it isn’t quite working as intended.
using UnityEngine;
using System.Collections;
public class CameraMove : MonoBehaviour {
public float moveSpeed;
public ShipInformation shipInfo;
public float clampedLength;
public float clampedHeight;
public float clampedWidth;
public void Start()
{
clampedLength = shipInfo.length * 0.5f;
clampedHeight = shipInfo.height * 0.5f;
clampedWidth = shipInfo.width * 0.5f;
}
public void Update()
{
Vector2 moveAmount = new Vector2(Input.GetAxis ("Horizontal"), Input.GetAxis("Vertical"));
transform.parent.Translate (transform.parent.right * -moveAmount.x
* moveSpeed);
transform.parent.Translate (transform.parent.forward * -moveAmount.y
* moveSpeed);
Debug.Log ("Move: " + moveAmount);
}
}
The clamped variables refer to length, width and height measurements of a given ship. It is halved in order to allow Mathf.Clamp to use each clamped dimension as high and low points, outside which the camera won’t be permitted to move.
I’ve tried a few options, such as using LateUpdate to attempt resetting the camera position after an update. Unfortunately, that doesn’t seem to be working.
My question is, how can I limit the Translate functions, preferably within the Update function, so that they won’t allow the camera to move outside the clamped dimensions?