- Home /
Mouse movement relative to screen space
I currently have code wherein I am able to move the camera based on the act of clicking down the left muse button and then dragging the camera. The problem lies in when the pointer is far away or close to the screen the movement is distorted.
Namely I want the mouse to function in a way so that when it is further away from the camera the momentum of moving the camera towards that point is stronger. Likewise pushing away from a close area would push the camera back stronger than if you were to do so from a further point.
I have a raycast for the sake of placing an object at the mouse position so I imagine I could also somehow use this information to determine the mouse's distance from the camera or to use it as an anchor point from which all momentum could be made. I just don't quite get how.
There is also a sub issue regarding my target. I am uncertain of how to make it keep the camera's rotation so as to ensure orbiting does not snap back to the forward vector.
using UnityEngine;
using System.Collections;
public class cameramove : MonoBehaviour {
public static cameramove Instance;
public float speed = 30f;
public float scrollspeed = 20f;
public float Rotspeed = 50f;
public Transform visual;
public Transform target;
public float scrollingClamp = 20.0f;
public Vector3 Distance = Vector3.forward * 20f;
public float DistanceMin = 7f;
public float DistanceMax = 15f;
private Vector3 Position = Vector3.zero;
private Vector3 NewPosition = Vector3.zero;
public float panSpeed = 10f;
public Vector3 mouseMove = Vector3.zero;
public Vector3 moving = Vector3.zero;
private Vector3 scrollingPos = Vector3.zero;
public Vector3 Height = new Vector3(0.0f, 3f, 0.0f);
public float YMinLimit = 5f;
public float YMaxLimit = 80f;
public float YHeight = 0f;
public float X_MouseSensitivity = 5f;
public float Y_MouseSensitivity = 5f;
CharacterController cmove;
// Use this for initialization
void Start () {
cmove = GetComponent<CharacterController>();
//Distance = Mathf.Clamp(Distance,DistanceMin, DistanceMax);
}
void Awake(){
Instance = this;
}
// Update is called once per frame
void LateUpdate () {
movingcamera();
scrolling();
turning ();
raycasting();
}
void movingcamera(){
//Moving the camera in relation to the world axis.
moving = Vector3.zero;
if(!Input.GetMouseButton(1))
{
if(Input.GetMouseButton(0))
{
moving = new Vector3(-Input.GetAxis("Mouse X") * panSpeed, 0,-Input.GetAxis("Mouse Y")* panSpeed);
target.transform.position = Camera.main.transform.position + Distance;
}
}
moving = transform.TransformDirection(moving);
moving *= speed;
moving = new Vector3(moving.x, 0.0f , moving.z);
cmove.Move(moving *Time.deltaTime);
}
void scrolling(){
scrollingPos = Vector3.zero;
//this use to work on a pure input basis but now it doesn't. Now it slowly accumulates value!
var deadZone = 0.01f;
if(Input.GetAxis("Mouse ScrollWheel") < deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone){
scrollingPos.y += Input.GetAxis("Mouse ScrollWheel") * scrollspeed;
}
cmove.Move(transform.forward *scrollingPos.y);
}
void turning(){
Screen.lockCursor = false;
Vector3 mouseMove = Vector3.zero;
if(Input.GetMouseButton(1)){
Screen.lockCursor = true;
mouseMove += new Vector3(Input.GetAxis("Mouse Y"),Input.GetAxis("Mouse X"),0.0f );
cmove.transform.LookAt(target);
mouseMove = transform.TransformDirection(mouseMove);
cmove.transform.position += mouseMove * speed *Time.deltaTime;
}
}
void raycasting()
{
RaycastHit vHit = new RaycastHit();
Ray vRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(vRay, out vHit, 1000))
{
Debug.Log("OK");
visual.transform.position = vHit.point +Height;
}
}
}