I am making the basic controls for a game that is mouse-oriented. I want the camera to follow the player and rotate around the player using the law of cosine to find an angle. Here is my code:
//Move Camera
camX = player.transform.position.x;
camY = 15.5f;
camZ = player.transform.position.z;
//Rotate Camera
rotTriA = player.transform.position.x;
rotTriB = player.transform.position.z;
rotTriC = Mathf.Sqrt((rotTriA * rotTriA) + (rotTriB + rotTriB));
if(rotTriA > 0 && rotTriB > 0)
{
getAngle = (Mathf.Acos(((rotTriA * rotTriA)
+ (rotTriB * rotTriB)
- (rotTriC * rotTriC))
/ (2 * (rotTriA) * (rotTriB))) * Mathf.Rad2Deg);
}
else if(rotTriA > 0 && rotTriB < 0)
{
getAngle = (Mathf.Acos(((rotTriA * rotTriA)
+ (rotTriB * rotTriB)
- (rotTriC * rotTriC))
/ (2 * (rotTriA) * (rotTriB))) * Mathf.Rad2Deg) + 90;
}
else if(rotTriA < 0 && rotTriB < 0)
{
getAngle = (Mathf.Acos(((rotTriA * rotTriA)
+ (rotTriB * rotTriB)
- (rotTriC * rotTriC))
/ (2 * (rotTriA) * (rotTriB))) * Mathf.Rad2Deg) + 180;
}
else if(rotTriA < 0 && rotTriB > 0)
{
getAngle = (Mathf.Acos(((rotTriA * rotTriA)
+ (rotTriB * rotTriB)
- (rotTriC * rotTriC))
/ (2 * (rotTriA) * (rotTriB))) * Mathf.Rad2Deg) + 270;
}
else if(rotTriA == 0 && rotTriB > 0)
{
getAngle = 90;
}
else if(rotTriA == 0 && rotTriB < 0)
{
getAngle = 270;
}
else if(rotTriA > 0 && rotTriB == 0)
{
getAngle = 0;
}
else if(rotTriA < 0 && rotTriB == 0)
{
getAngle = 180;
}
else
{
getAngle = 0;
}
Debug.Log(getAngle);
if (camZ < 0)
{
camZ = player.transform.position.z + 10.5f;
transform.eulerAngles = new Vector3(135f, getAngle, 0.0f);
}
else
{
camZ = player.transform.position.z - 10.5f;
transform.eulerAngles = new Vector3(45f, getAngle, 0.0f);
}
camZ = player.transform.position.z - 15.5f;
cam = new Vector3(-camX, camY, camZ);
mainCamera.transform.position = cam;
I know how to make the camera follow the player, but I don't know how to make the camera rotate around the player without it freaking out.
Possible reasons why this is occurring: I am using raycasting to move the player, so when I move my mouse outside of the arena, the movements wont register, I might not be doing the law of cosine right because I get values up to 400 (which shouldn't be possible, given a circle is 360 degrees).
The easiest way to make a camera rotate around the player would be to take advantage of the game object heirarchy.
First make an empty GameObject and then make your camera a child of it:
EmptyObject
> Camera
Now orient your camera to look at the position of the empty object. The location of this empty object is now where your camera is "Looking". Since the camera is a child, if you rotate this object, the camera rotates around it (while maintaining the orientation of facing the empty object).
A simple script to rotate the empty object will, thanks to transform parenting, make the camera "orbit" around it while looking at it:
using UnityEngine;
public class CameraInput : MonoBehaviour
{
[SerializeField] private float rotationAcceleration; // radians/(frame^2)
[SerializeField] private float rotationSpeedMax; // radians/frame
[SerializeField] private float rotationDamping; // 0.99
private float rotationSpeed;
private void Awake()
{
rotationSpeed = rotationSpeedMax / 4;
}
private void Update()
{
if (Input.GetKey(KeyCode.A))
{
rotationSpeed += rotationAcceleration;
}
if (Input.GetKey(KeyCode.D))
{
rotationSpeed -= rotationAcceleration;
}
rotationSpeed = Mathf.Clamp(
rotationSpeed, -rotationSpeedMax, rotationSpeedMax);
transform.Rotate(0f, rotationSpeed, 0f);
rotationSpeed *= rotationDamping;
}
}
Attaching something like this to the EmptyObject object will do the trick. Now you just need this empty object to follow your character. The rotational functions accept radians as arguments so you'll want to use small [0.00 ~ 0.01] numbers. Also, this is a per-frame implementation so it'll only work smoothly while your frame rate is smooth; you'd want to eventually have Time.deltaTime factor into it.
Related
So im trying to make my plane gain speed when decreasing altitude and lose it when going up.
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using Random = UnityEngine.Random;
[RequireComponent(typeof(Rigidbody))]
public class Movement : MonoBehaviour
{
public float AmbientSpeed = 400.0f;
public float RotationSpeed = 275.0f;
private Rigidbody _rigidBody;
bool IsInputEnabled = true;
void Start()
{
_rigidBody = GetComponent<Rigidbody>();
}
void Update()
{
if (IsInputEnabled)
{
// Slow down
if (Input.GetKey("z"))
{
AmbientSpeed--;
if (AmbientSpeed < 0)
AmbientSpeed = 0;
}
// Speed up
if (Input.GetKey("x"))
{
AmbientSpeed++;
if (AmbientSpeed > 400)
AmbientSpeed = 400;
}
}
}
void FixedUpdate()
{
Quaternion AddRot = Quaternion.identity;
float roll = 0;
float pitch = 0;
float yaw = 0;
bool stall = false;
// Getting pitch angle
var right = transform.right;
right.y = 0;
right *= Mathf.Sign(transform.up.y);
var fwd = Vector3.Cross(right, Vector3.up).normalized;
float pitch_rot = Vector3.Angle(fwd, transform.forward) * Mathf.Sign(transform.forward.y);
// Engine stall
if (IsInputEnabled)
{
Quaternion targetpos = new Quaternion(0.5f, AddRot.y, AddRot.z, AddRot.w);
if (AmbientSpeed < 25)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetpos, 50 * Time.deltaTime);
}
if (stall == true && AmbientSpeed == 0 && AmbientSpeed < 100)
{
if (AmbientSpeed < 100)
{
AmbientSpeed = AmbientSpeed + 1;
}
if (AmbientSpeed > 100)
{
stall = false;
}
}
}
if (IsInputEnabled)
{
roll = Input.GetAxis("Roll") * (Time.fixedDeltaTime * RotationSpeed);
pitch = Input.GetAxis("Pitch") * (Time.fixedDeltaTime * RotationSpeed);
yaw = Input.GetAxis("Yaw") * (Time.fixedDeltaTime * RotationSpeed);
AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
_rigidBody.rotation *= AddRot;
Vector3 AddPos = Vector3.forward;
AddPos = _rigidBody.rotation * AddPos;
_rigidBody.velocity = AddPos * (Time.fixedDeltaTime * AmbientSpeed);
if (Vector3.Dot(transform.up, Vector3.down) > 0)
{
_rigidBody.AddForce(0, 0, -10);
}
if (pitch_rot > 30)
{
AmbientSpeed = AmbientSpeed - 0.001f;
if (AmbientSpeed < 0)
{
AmbientSpeed = 0f;
}
}
if (pitch_rot > 50 && AmbientSpeed < 200)
{
AmbientSpeed = AmbientSpeed - 10;
if (AmbientSpeed < 0)
{
AmbientSpeed = 0;
stall = true;
}
Debug.Log("Stalling");
}
}
}
}
Here is my code in C# The parts used for movement are in FixedUpdate. Just confused as how to get a smooth increase / decrease in AmbientSpeed when at steep angles. Any help appreciated. I'm quite new to Unity and c# so sorry if the code is a bit cluttered.
Here is the code with my attempt at comments not sure how useful they will be as alot of code is just taken from forum and the scripting page API document thing.
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using Random = UnityEngine.Random;
[RequireComponent(typeof(Rigidbody))]
public class Movement : MonoBehaviour
{
// Declare the max values
public float AmbientSpeed = 400.0f;
public float RotationSpeed = 275.0f;
private Rigidbody _rigidBody;
bool IsInputEnabled = true; //used in engine stall
void Start()
{
_rigidBody = GetComponent<Rigidbody>();
}
void Update()
{
if (IsInputEnabled)
{
// Slow down the plane when Z is held
if (Input.GetKey("z"))
{
AmbientSpeed--;
if (AmbientSpeed < 0) // Make sure min speed isnt broken
AmbientSpeed = 0;
}
// Speed up the plane when x is held
if (Input.GetKey("x"))
{
AmbientSpeed++;
if (AmbientSpeed > 400) // Make sure max speed isnt broken
AmbientSpeed = 400;
}
}
}
void FixedUpdate()
{
Quaternion AddRot = Quaternion.identity; // Sets current Quaternion to AddRot variable
float roll = 0;
float pitch = 0;
float yaw = 0;
bool stall = false;
// Getting pitch angle
var right = transform.right;
right.y = 0;
right *= Mathf.Sign(transform.up.y);
var fwd = Vector3.Cross(right, Vector3.up).normalized;
float pitch_rot = Vector3.Angle(fwd, transform.forward) * Mathf.Sign(transform.forward.y); // Complex math i don't understand
// Engine stall
if (IsInputEnabled)
{
Quaternion targetpos = new Quaternion(0.5f, AddRot.y, AddRot.z, AddRot.w); // Set target pos to a declining pitch
if (AmbientSpeed < 25)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetpos, 50 * Time.deltaTime); // when engine gets below 25 speed then rotate towards the targetpos
}
if (stall == true && AmbientSpeed < 100) // if we are stalling
{
AmbientSpeed = AmbientSpeed + 1; // Once stall is reached and plane is in decline the speed will increase due to steep decline
if (AmbientSpeed > 100)
{
stall = false;
}
}
}
if (IsInputEnabled)
{
roll = Input.GetAxis("Roll") * (Time.fixedDeltaTime * RotationSpeed);
pitch = Input.GetAxis("Pitch") * (Time.fixedDeltaTime * RotationSpeed); // User input for Pitch/roll/yaw
yaw = Input.GetAxis("Yaw") * (Time.fixedDeltaTime * RotationSpeed);
AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll); // Sets Addrot to currect pitch/yaw/roll
_rigidBody.rotation *= AddRot;
Vector3 AddPos = Vector3.forward;
AddPos = _rigidBody.rotation * AddPos; // rotates rigid body to addrot
_rigidBody.velocity = AddPos * (Time.fixedDeltaTime * AmbientSpeed);
if (Vector3.Dot(transform.up, Vector3.down) > 0) // If upside down then fall
{
_rigidBody.AddForce(0, 0, -10);
}
if (pitch_rot > 30) // If pitch too far up lose speed
{
AmbientSpeed = AmbientSpeed - 0.001f;
if (AmbientSpeed < 0)
{
AmbientSpeed = 0f;
}
}
if (pitch_rot > 50 && AmbientSpeed < 200) // if go too far up pitch and lose too much speed you will stall
{
AmbientSpeed = AmbientSpeed - 10;
if (AmbientSpeed < 0)
{
AmbientSpeed = 0;
stall = true; // stall is true and input will be disabled
}
Debug.Log("Stalling");
}
}
}
}
I am trying to roll a ball using gravity. I am also trying to rotate the ground using the accelerometer but the ground is shaky when it rotates. I also can't set a limit on the rotation of the x Axis.
void Update()
{
tilt = new Vector3(Input.acceleration.x, Input.acceleration.y, Mathf.Clamp(Input.acceleration.z,-15,15));
tilt = Quaternion.Euler(90, 0, 90) * tilt; // Change Rotation according to camera view
if(tilt.x > 0.05f || tilt.x < -0.05f && tilt.x > -6f || tilt.x < 6f)
{
transform.localRotation = new Quaternion(-tilt.x, 0, 0, 3);
}
if (tilt.z < -0.1f || tilt.z >0.1f)
{
transform.localRotation = new Quaternion(0, 0, -tilt.z, 3);
}
}
See if this helps. This is a simple example of many ways to implement angular velocity.
Please be advised that Gimbel's lock effect is not considered in calculations. You might want to restrict the rotation in a certain range. Also you will need a dead zone for delta, e.g. if(delta < epsilon) delta = 0;.
float speed = 40f;
void Update()
{
tilt = new Vector3(Input.acceleration.x, Input.acceleration.y, Mathf.Clamp(Input.acceleration.z,-15,15));
tilt = Quaternion.Euler(90, 0, 90) * tilt; // Change Rotation according to camera view
if(tilt.x > 0.05f || tilt.x < -0.05f && tilt.x > -6f || tilt.x < 6f)
{
var delta = -tilt.x - transform.localRotation.x;
transform.localRotation += (new Quaternion(delta, 0, 0, 3) * Time.deltaTime * speed);
}
if (tilt.z < -0.1f || tilt.z >0.1f)
{
var delta = -tilt.z - transform.localRotation.z;
transform.localRotation += (new Quaternion(0, 0, -tilt.z, 3) * Time.deltaTime * speed);
}
}
I made the precursor to my 3d Agario game. It is a mouse-oriented game. You use the mouse to control most of the movement. This includes the rotation of the Camera.
My problem is the Camera won't stop rotating. This is because it is very hard to center the mouse to the center of the screen, and the way I made the Camera rotate is by using a Sine equation.
Here is my code:
//public
public GameObject player;
public float rotationSpeed;
//v3
private Vector3 mousePos;
private Vector3 playerPos;
private Vector3 camPos;
//float
private float sideX;
private float sideZ;
private float sideC;
private float camX;
private float camZ;
private float camC;
private float rotX;
private float rotZ;
private float rotC;
private float rotAngle;
private Quaternion currRotation;
//camera
private Camera hitCam;
void Start ()
{
transform.rotation = Quaternion.Euler(30, 0, 0);
transform.position = new Vector3(0, 15, 0);
}
void LateUpdate ()
{
mousePos = HitCameraController.mousePos;
playerPos = player.transform.position;
sideX = (mousePos.x - playerPos.x);
sideZ = (mousePos.z - playerPos.z);
sideC = (Mathf.Sqrt(Mathf.Pow(sideX, 2) + Mathf.Pow(sideZ, 2)));
camC = 15;
camX = (camC * sideX) / sideC;
camZ = (camC * sideZ) / sideC;
rotX = (sideX + camX);
rotZ = (sideZ + camZ);
rotC = (Mathf.Sqrt(Mathf.Pow(rotX, 2) + Mathf.Pow(rotZ, 2)));
if (rotX >= 0 && rotZ >= 0)
{
rotAngle = (Mathf.Asin(rotX / rotC) * Mathf.Rad2Deg);
}
if (rotX >= 0 && rotZ <= 0)
{
rotAngle = 180 - (Mathf.Asin(rotX / rotC) * Mathf.Rad2Deg);
}
if (rotX <= 0 && rotZ >= 0)
{
rotAngle = (Mathf.Asin(rotX / rotC) * Mathf.Rad2Deg);
}
if (rotX <= 0 && rotZ <= 0)
{
rotAngle = -180 - (Mathf.Asin(rotX / rotC) * Mathf.Rad2Deg);
}
camPos = new Vector3((playerPos.x - camX), 15, (playerPos.z - camZ));
currRotation = Quaternion.Euler(30, rotAngle, 0);
// Move Camera
transform.position = camPos;
// Rotate Camera
/*transform.rotation = Quaternion.RotateTowards(
transform.rotation, currRotation,
(rotationSpeed * Time.deltaTime));*/
transform.rotation = currRotation;
}
I want to make a sort of "safe-zone" that will prevent the calculation of the rotating angle. For instance, if I keep my mouse within a certain boundary in the center, the Camera won't rotate.
I tried to make the boundary using an "or" statement, but it didn't give me the result I was looking for (the camera would still rotate even when my mouse was in the boundary. The Camera would stop on some tries, but it would jitter immensely before doing so.)
I know exactly why this is occurring: the ray isn't being cast on a still camera; hence, the mouse coordinate will always change, even if I don't move the mouse. I tried casting the ray to the "Hit" camera (the still Camera), but the follow camera couldn't pick up on the mouse movement. I don't know how to fix this problem other than to keep brainstorming for an intuitive solution, but my brain can only innovate so much.
If you could think of a solution before me, let me know by any means.
[...]
void LateUpdate ()
{
mousePos = HitCameraController.mousePos;
playerPos = player.transform.position;
sideX = (mousePos.x - playerPos.x);
sideZ = (mousePos.z - playerPos.z);
sideC = (Mathf.Sqrt(Mathf.Pow(sideX, 2) + Mathf.Pow(sideZ, 2)));
// New Stuff
sideC -= 5f; // substract the safe-zone-radius
if(sideC < 0f)
{
// Inside safe-zone, do nothing.
sideC = 0f;
sideX = 0f;
sideZ = 0f;
}else{
// Outside safe-zone, correct values to avoid jumping.
float x_ratio = sideX / sideZ; // be careful with "divide by zero"
float z_ratio = sideZ / sideX;
sideX -= 5 * x_ratio;
sideZ -= 5 * z_ratio;
}
// End New Stuff
camC = 15;
camX = (camC * sideX) / sideC;
camZ = (camC * sideZ) / sideC;
rotX = (sideX + camX);
rotZ = (sideZ + camZ);
rotC = (Mathf.Sqrt(Mathf.Pow(rotX, 2) + Mathf.Pow(rotZ, 2)));
if (rotX >= 0 && rotZ >= 0)
{
rotAngle = (Mathf.Asin(rotX / rotC) * Mathf.Rad2Deg);
}
if (rotX >= 0 && rotZ <= 0)
{
rotAngle = 180 - (Mathf.Asin(rotX / rotC) * Mathf.Rad2Deg);
}
if (rotX <= 0 && rotZ >= 0)
{
rotAngle = (Mathf.Asin(rotX / rotC) * Mathf.Rad2Deg);
}
if (rotX <= 0 && rotZ <= 0)
{
rotAngle = -180 - (Mathf.Asin(rotX / rotC) * Mathf.Rad2Deg);
}
camPos = new Vector3((playerPos.x - camX), 15, (playerPos.z - camZ));
currRotation = Quaternion.Euler(30, rotAngle, 0);
I'm trying to get a unity C# script working that will rotate the camera around the X axis in a 3D environment. Currently, it flips the screen making my terrain look like it is hanging upside down. I'm just trying to get the camera to rotate on the X axis instead. Below is what I currently have.
using UnityEngine;
public class TouchCamera : MonoBehaviour {
Vector2?[] oldTouchPositions = {
null,
null
};
Vector2 oldTouchVector;
float oldTouchDistance;
void Update() {
if (Input.touchCount == 0) {
oldTouchPositions[0] = null;
oldTouchPositions[1] = null;
}
else if (Input.touchCount == 1) {
if (oldTouchPositions[0] == null || oldTouchPositions[1] != null) {
oldTouchPositions[0] = Input.GetTouch(0).position;
oldTouchPositions[1] = null;
}
else {
Vector2 newTouchPosition = Input.GetTouch(0).position;
transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] - newTouchPosition) * camera.orthographicSize / camera.pixelHeight * 2f));
oldTouchPositions[0] = newTouchPosition;
}
}
else {
if (oldTouchPositions[1] == null) {
oldTouchPositions[0] = Input.GetTouch(0).position;
oldTouchPositions[1] = Input.GetTouch(1).position;
oldTouchVector = (Vector2)(oldTouchPositions[0] - oldTouchPositions[1]);
oldTouchDistance = oldTouchVector.magnitude;
}
else {
Vector2 screen = new Vector2(camera.pixelWidth, camera.pixelHeight);
Vector2[] newTouchPositions = {
Input.GetTouch(0).position,
Input.GetTouch(1).position
};
Vector2 newTouchVector = newTouchPositions[0] - newTouchPositions[1];
float newTouchDistance = newTouchVector.magnitude;
transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] + oldTouchPositions[1] - screen) * camera.orthographicSize / screen.y));
transform.localRotation *= Quaternion.Euler(new Vector3(0, 0, Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f));
camera.orthographicSize *= oldTouchDistance / newTouchDistance;
transform.position -= transform.TransformDirection((newTouchPositions[0] + newTouchPositions[1] - screen) * camera.orthographicSize / screen.y);
oldTouchPositions[0] = newTouchPositions[0];
oldTouchPositions[1] = newTouchPositions[1];
oldTouchVector = newTouchVector;
oldTouchDistance = newTouchDistance;
}
}
}
}
If you wanted to rotate it around (like a character turning its head), in the x-z plain, then you wanted to rotate it around the y axis, not the x.
In the end, I had to make a raycast shoot out the center of the camera and when it collided with something, set that as the pivot point. Then I rotated the camera around said pivot point. Example below...
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 300)){}
float distanceToGround = hit.distance;
var pivotPoint = hit.point;
// -------- Rotation ---------
transform.RotateAround(pivotPoint, Vector3.up,Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f);
//---------------------------------------------------------
I'm hoping there's someone out there that can help me with a small problem.
Currently I have an Input Manager attached to the main camera to allow the user to pan around the map by moving the mouse to the edges of the window, but I've encountered a slight problem which I've tried to fix myself to no avail.
If the mouse goes outside of the window, the panning still happens, which I find irritating when I'm debugging or using other applications. So I am hoping that someone can help me to stop the movement happening when the mouse is outside the game window.
Here is the code for my Input Manager.
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour {
public Vector3 position = new Vector3(0,0, -10);
public int boundary = 50;
public int speed = 4;
private int screenBoundsWidth;
private int screenBoundsHeight;
// Use this for initialization
void Start()
{
screenBoundsWidth = Screen.width;
screenBoundsHeight = Screen.height;
}
// Update is called once per frame
void Update()
{
if (Input.mousePosition.x > screenBoundsWidth - boundary) {
position.x += speed * Time.deltaTime;
}
if (Input.mousePosition.x < 0 + boundary) {
position.x -= speed * Time.deltaTime;
}
if (Input.mousePosition.y > screenBoundsHeight - 10) {
position.y += speed * Time.deltaTime;
}
if (Input.mousePosition.y < 0 + boundary) {
position.y -= speed * Time.deltaTime;
}
Camera.mainCamera.transform.position = position;
}
}
Thank you for your time.
EDIT
I have come up with a hacky work around, but it still causes the movement to happen in certain locations around the outside of the window. I am hoping someone can come up with a better solution.
if (Input.mousePosition.x < screenBoundsWidth && Input.mousePosition.y < screenBoundsHeight) {
if (Input.mousePosition.x > screenBoundsWidth - boundary) {
position.x += speed * Time.deltaTime;
}
}
if (Input.mousePosition.x > 0 && Input.mousePosition.y > 0) {
if (Input.mousePosition.x < 0 + boundary) {
position.x -= speed * Time.deltaTime;
}
}
if (Input.mousePosition.y < screenBoundsHeight && Input.mousePosition.x < screenBoundsWidth) {
if (Input.mousePosition.y > screenBoundsHeight - 22) {
position.y += speed * Time.deltaTime;
}
}
if (Input.mousePosition.y > 0 && Input.mousePosition.x > 0) {
if (Input.mousePosition.y < 0 + boundary) {
position.y -= speed * Time.deltaTime;
}
}
3 Ideas:
Rect screenRect = new Rect(0,0, Screen.width, Screen.height);
if (!screenRect.Contains(Input.mousePosition))
return;
The same can be written more verbously as:
float mouseX = Input.MousePosition.x;
float mouseY = Input.MousePosition.y;
float screenX = Screen.width;
float screenY = Screen.height;
if (mouseX < 0 || mouseX > screenX || mouseY < 0 || mouseY > screenY)
return;
// your Update body
...which is pretty much the same as your "hacky" solution (which is completely valid imho).
Another option is to create 4 Rect objects for each screen border, then check if mouse is inside those rects. Example:
public float boundary = 50;
public float speed = 4;
private Rect bottomBorder;
private Rect topBorder;
private Transform cameraTransform;
private void Start()
{
cameraTransform = Camera.mainCamera.transform
bottomBorder = new Rect(0, 0, Screen.width, boundary);
topBorder = new Rect(0, Screen.height - boundary, Screen.width, boundary);
}
private void Update()
{
if (topBorder.Contains(Input.mousePosition))
{
position.y += speed * Time.deltaTime;
}
if (bottomBorder.Contains(Input.mousePosition))
{
position.y -= speed * Time.deltaTime;
}
cameraTransform.position = position;
}
The tricky part here is that Rect coordinates have Y axis pointing down and Input.mousePosition has Y pointing up... so bottomBorder Rect has to be on the top, and topBorder has to be at the bottom. Left and right borders are not affected.
Due to the way Unity and the various host operating systems interact, you have limited control of the mouse cursor. (in short the OS controls the mouse Unity just reads it) That being said you do have some options. Screen.lockCursor jumps to mind.
http://docs.unity3d.com/Documentation/ScriptReference/Screen-lockCursor.html
It won't do exactly what you are looking for but it might be a good starting point
Time.speed = 0;
is this what you want?