Onto lesson 2
This commit is contained in:
50
Assets/Scripts/CameraController.cs
Normal file
50
Assets/Scripts/CameraController.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Scripting.APIUpdating;
|
||||
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
public float moveSpeed;
|
||||
public float zoomSpeed;
|
||||
public float minZoomDist;
|
||||
public float maxZoomDist;
|
||||
private Camera cam;
|
||||
void Awake()
|
||||
{
|
||||
cam = Camera.main;
|
||||
}
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
Move();
|
||||
Zoom();
|
||||
}
|
||||
|
||||
private void Move()
|
||||
{
|
||||
float xInput = Input.GetAxisRaw("Horizontal");
|
||||
float zInput = Input.GetAxisRaw("Vertical");
|
||||
Vector3 dir = transform.forward * zInput + transform.right * xInput;
|
||||
transform.position += dir * moveSpeed * Time.deltaTime;
|
||||
}
|
||||
private void Zoom()
|
||||
{
|
||||
float scrollInput = Input.GetAxisRaw("Mouse ScrollWheel");
|
||||
float dist = Vector3.Distance(transform.position, cam.transform.position);
|
||||
if(dist < minZoomDist && scrollInput > 0.0f)
|
||||
return;
|
||||
if(dist > maxZoomDist && scrollInput < 0.0f)
|
||||
return;
|
||||
cam.transform.position += cam.transform.forward * scrollInput * zoomSpeed;
|
||||
}
|
||||
public void FocusOnPosition(Vector3 pos)
|
||||
{
|
||||
transform.position = pos;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user