using UnityEngine;
using System;
using System.Collections;
public class RTScontroller : MonoBehaviour
{
int lastScroll = 0; // last screen’s scroll, used for velocity
float ZOOM_ANGLE = 6; // denominator of the radian degree used for zooming
float wheelVelocity = 0; // used for zoom velocity
float thisScroll; // float thisScroll = Input.GetAxis(“Mouse ScrollWheel”);
float horzKeys; // horizontal key movent relative to scrollSpeed
float vertKeys; // vertical movement relative to scroll
float yScroll; // = Input.GetAxis(“Mouse ScrollWheel”); //store input, used primarily for +/-
float forwardScroll; // = Input.GetAxis(“Mouse ScrollWheel”);// same ^^
public const int MIN_Y = 40;
public const int MAX_Y = 400;
public const int scrollSpeed = 15; // Camera’s movement speed
public const float VELOCITY_SCALE = 1; // used to scale zoom velocity
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
// mouse move method
// do movement by mousePosition
// screen Right
if (Input.mousePosition.x > Screen.width) { transform.Translate(Vector3.right * scrollSpeed); }
//screen left
if (Input.mousePosition.x < 0) { transform.Translate(Vector3.left * scrollSpeed); }
// screen up
if (Input.mousePosition.y > Screen.height) { transform.Translate(Vector3.forward * scrollSpeed); }
//screen down
if (Input.mousePosition.y < 0) { transform.Translate(Vector3.forward * -scrollSpeed); }
//keybord move method
// Do camera movement by keyboard
horzKeys = (Input.GetAxis(“Horizontal”) * scrollSpeed);
vertKeys = (Input.GetAxis(“Vertical”) * scrollSpeed);
transform.Translate(horzKeys, 0, vertKeys);
// Do zoom by mouse wheel
//thisScroll = Input.GetAxis(“Mouse ScrollWheel”);
velocityUpdate(); // Updates wheel velocity in order to continue acceleration of zoom feature
float rotationSpeed = ((float)Math.PI * (float)wheelVelocity) / ZOOM_ANGLE; // use wheelVelocity to get correct angle scale and direction
/* because the change in camera height is dependant on wheelVelocity, so must be the angle at which we rotate during the zoom.
if we multiply the angle by the velocity, the angle should rotate the same relative to the speed of the scroll*/
while ( transform.position.y > MIN_Y transform.position.y < MAX_Y )
{
//yScroll = Input.GetAxis(“Mouse ScrollWheel”) * wheelVelocity;
forwardScroll = Input.GetAxis(“Mouse ScrollWheel”) * wheelVelocity;
transform.Translate(0, Input.GetAxis(“Mouse ScrollWheel”) * wheelVelocity, Input.GetAxis(“Mouse ScrollWheel”) * wheelVelocity);
if (thisScroll > 0) { transform.Rotate((Vector3.up * rotationSpeed)); }
if (thisScroll < 0) { transform.Rotate((Vector3.up * rotationSpeed)); }
}
}
/*
RTScontroller.velocityUpdate() : currently private
use : accelerated zoom feature for game camera
takes an input reading from a mouse wheel ( -1 < x < 1 ) and increments the wheelVelocity
complete stop is recognized as 2 frames of no movement
use VELOCITY_SCALE to increase the ammount of acceleration
*/
void velocityUpdate()
{
if ((lastScroll == 0) (thisScroll == 0)) { wheelVelocity = 0; }// if no scroll, reset velocity to zero
else
{ //prioritize input decisions by clearly seperating no scroll option
if (thisScroll != 0)
{ // if thisScroll isnt zero, the following will compound velocity
if (lastScroll > 0 thisScroll > 0) { wheelVelocity++; }; // if positive scroll, increase velocity
if (lastScroll > 0 thisScroll < 0) { wheelVelocity = -1; }; // quickly switches to opposite scroll
if (lastScroll < 0 thisScroll < 0) { wheelVelocity–; }; // if negative scroll, decrement velocity
if (lastScroll < 0 thisScroll > 0) { }
}
else { wheelVelocity = lastScroll; } // if this scroll is zero, prepare to stop. used for soft deceleration
}
// end the funtion by adding a constant to scale velocity effect
wheelVelocity = wheelVelocity * VELOCITY_SCALE;
}
//end class
}