Camera.main.transform.position = Vector3.MoveTowards (origin, destination, Time.deltaTime * ResourceManager.ScrollSpeed);
where scroll speed = 25
destination is a vector with x,z,y
on a debug.log destination was -54
here is the full code:
using UnityEngine;
using System.Collections;
using RTS;
public class UserInput : MonoBehaviour {
//sets up a private variable for only this class - our player
private Player player;
// Use this for initialization
void Start () {
//this goes to the root of the player ie the object player and allows us to interact with it
player = transform.root.GetComponent< Player > ();
}//end Start()
// Update is called once per frame
void Update () {
//if the player is human
if(player.human){
//activates move camera and rotate camera methods constantly to check whether the user wants to scroll/rotate
MoveCamera();
RotateCamera();
}//end if
}//end update()
private void MoveCamera(){
//gets the position of the y and x axis of the mouse position
float xpos = Input.mousePosition.x;
float ypos = Input.mousePosition.y;
//sets up a 3 dimensional vector
Vector3 movement = new Vector3 (0, 0, 0);
//horizontal camera movement
//if the x position is greater or equal to 0 and smaller than the scroll width(ie it must be near the edge)
if (xpos >= 0 && xpos < ResourceManager.ScrollWidth) {
movement.x -= ResourceManager.ScrollSpeed;
//same as above but this checks whether it is going to opposite direction
}else if(xpos <= Screen.width && xpos > Screen.width - ResourceManager.ScrollWidth){
movement.x += ResourceManager.ScrollSpeed;
}//end if
//vertical camera movement
//same as all above
if(ypos >= 0 && ypos < ResourceManager.ScrollWidth){
movement.z -= ResourceManager.ScrollSpeed;
}else if(ypos <= Screen.height && ypos > Screen.height - ResourceManager.ScrollWidth){
movement.z += ResourceManager.ScrollSpeed;
}
//this makes sure that movement is in the direction the camera is facing
movement = Camera.main.transform.TransformDirection (movement);
//ignores the vertical axis so it does not have weird up and down movements - easier to do with scroll wheel on mouse
movement.y = 0;
//This is away from the ground movement
movement.y -= ResourceManager.ScrollSpeed * Input.GetAxis ("Mouse ScrollWheel");
//Debug.Log (movement.y);
//Debug.Log (Input.GetAxis ("Mouse ScrollWheel"));
//calculate desired camera position based on received input
Vector3 origin = Camera.main.transform.position;
Vector3 destination = origin;
destination.x += movement.x;
destination.y += movement.y;
destination.z += movement.z;
if (destination.y > ResourceManager.MaxCameraHeight) {
destination.y = ResourceManager.MaxCameraHeight;
} else if (destination.y < ResourceManager.MinCameraHeight) {
destination.y = ResourceManager.MinCameraHeight;
}
//if a change in position is detected perform the necessary update
if (destination != origin) {
Camera.main.transform.position = Vector3.MoveTowards (origin, destination, Time.deltaTime * ResourceManager.ScrollSpeed);
}
}//end MoveCamera()
private void RotateCamera(){
}//end void
}//end class
using UnityEngine;
using System.Collections;
namespace RTS{
public static class ResourceManager {
//Setting up the scroll speed and the rotate speed
public static float ScrollSpeed { get { return 25; } }
public static float RotateSpeed { get { return 100; } }
//This is how many pixels from the edge of the screen the mouse needs to be scroll
public static int ScrollWidth { get { return 15; } }
public static float MinCameraHeight { get { return 10; } }
public static float MaxCameraHeight { get { return 10; } }
}
}