So in my camera script I added a section where if the player pressed numpad 5, the camera will snap to a greater y axis, and on a 95 degree rotation. The problem is, it won’t work and I don’t know why. Please Help! P.S. Please excuse any bad math on my part in my script section, I did this late at night, but It would help if u could point it out if there is any.
Section of script which is dealing with this : Quaternion CameraRotSnap;
void Start()
{
MovableObjScript = GameObject.Find("MoveObjectsController").GetComponent<PlayerMovableObjectsManager>();
}
void Update()
{
float CameraPosSnapX = Camera.main.transform.position.x;
float CameraPosSnapY = Camera.main.transform.position.y;
float CameraPosSnapZ = Camera.main.transform.position.z;
float CameraRotSnapX = Camera.main.transform.rotation.x;
float CameraRotSnapZ = Camera.main.transform.rotation.z;
if (Input.GetButtonDown("[5]"))
{
CameraPosSnapX = transform.position.x;
CameraPosSnapY = transform.position.y + 10;
CameraPosSnapZ = transform.position.z;
CameraRotSnap = Quaternion.Euler(CameraRotSnapX, 95, CameraRotSnapZ);
}
Rest of script in case of conflict: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
public float dragSpeed = 1f;
private Vector3 dragOrigin;
float MouseRotateSpeed = 3;
float SelectionBoxWidth;
float SelectionBoxHeight;
float SelectionBoxTop;
float SelectionBoxLeft;
Vector2 SelectionBoxStart;
Vector2 SelectionBoxFinish;
Vector3 SelectionBoxMouseDownPoint;
Vector3 SelectionBoxCurrentMousePos;
private PlayerMovableObjectsManager MovableObjScript;
Quaternion CameraRotSnap;
void Start()
{
MovableObjScript = GameObject.Find("MoveObjectsController").GetComponent<PlayerMovableObjectsManager>();
}
void Update()
{
float CameraPosSnapX = Camera.main.transform.position.x;
float CameraPosSnapY = Camera.main.transform.position.y;
float CameraPosSnapZ = Camera.main.transform.position.z;
float CameraRotSnapX = Camera.main.transform.rotation.x;
float CameraRotSnapZ = Camera.main.transform.rotation.z;
if (Input.GetButtonDown("[5]"))
{
CameraPosSnapX = transform.position.x;
CameraPosSnapY = transform.position.y + 10;
CameraPosSnapZ = transform.position.z;
CameraRotSnap = Quaternion.Euler(CameraRotSnapX, 95, CameraRotSnapZ);
}
SelectionBoxCurrentMousePos = Input.mousePosition;
if (Input.GetMouseButtonDown(1))
{
SelectionBoxMouseDownPoint = Input.mousePosition;
}
//GUI Vars
SelectionBoxWidth = Camera.main.WorldToScreenPoint(SelectionBoxMouseDownPoint).x - Camera.main.WorldToScreenPoint(SelectionBoxCurrentMousePos).x;
SelectionBoxHeight = Camera.main.WorldToScreenPoint(SelectionBoxMouseDownPoint).y - Camera.main.WorldToScreenPoint(SelectionBoxCurrentMousePos).y;
SelectionBoxLeft = Input.mousePosition.x;
SelectionBoxTop = (Screen.height - Input.mousePosition.y) - SelectionBoxHeight;
//Other Mouse Stuff
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
if (Input.GetMouseButton(1))
{
transform.RotateAround(transform.position, transform.right, -pos.y * MouseRotateSpeed);
transform.RotateAround(transform.position, Vector3.up, pos.x * MouseRotateSpeed);
// transform.RotateAround(transform.position, transform.up, -pos.y * MouseRotateSpeed);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
GetComponent<Transform>().position = new Vector3(transform.position.x, transform.position.y - .3f, transform.position.z + .2f);
// transform.Rotate(-2, 0, 0);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
GetComponent<Transform>().position = new Vector3(transform.position.x, transform.position.y + .3f, transform.position.z - .2f);
// transform.Rotate(2, 0, 0);
}
if (Input.GetMouseButtonDown(0))
{
dragOrigin = Input.mousePosition;
return;
}
if (!Input.GetMouseButton(0)) return;
Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);
transform.Translate(move, Space.World);
}
void OnGUI()
{
if (Input.GetMouseButton(2))
{
GUI.Box(new Rect(SelectionBoxLeft, SelectionBoxTop, SelectionBoxWidth, SelectionBoxHeight), "");
}
}
}