- Home /
Change target position of mouse orbit
I'm working with an edited version of the "mouse orbit improved" script (http://wiki.unity3d.com/index.php?title=MouseOrbitImproved). The target of the mouse orbit is set to a cube for testing purposes. Basically what I'm trying to do is give the player the ability to right click and change the position of the target (thus changing the pivot point of the orbit cam). I've managed to get the target to move around, but as far as I can tell it moves randomly relative to where I right click over my walking plane. However, if I print the hit data from the raycast I send out, it seems like numerically it IS placing the target transform at the correct position (i.e., if I get back a raycast of 1,2,0, the target cube moves to 1,20 - the problem is that this location is not where I have actually clicked on the screen). Something is clearly going on mathematically which I don't understand.
The relevant functions below are Update, SetTargetPosition, and Move.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour {
public Transform target;
Vector3 desiredPosition;
public float distance = 15.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = 0f;
public float yMaxLimit = 80f;
public float distanceMin = 1f;
public float distanceMax = 30f;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
public Vector3 startingRot = new Vector3(45, 180, 0);
// Use this for initialization
void Start ()
{
distance = 15f;
xSpeed = 0f;
ySpeed = 0f;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
x = startingRot.y;
y = startingRot.x;
}
void Update () {
if (Input.GetMouseButtonUp(1))
{
SetTargetPosition();
}
}
void SetTargetPosition()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
desiredPosition = hit.point;
Debug.Log(desiredPosition);
}
Move();
}
void Move()
{
target.position = desiredPosition;
}
public void resetZoom ()
{
distance = 15f;
}
public void stopRotating () {
xSpeed = 0f;
ySpeed = 0f;
}
public void resumeRotating () {
//being used?
xSpeed = 40f;
ySpeed = 80f;
}
void LateUpdate ()
{
if (target)
{
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
if (target && Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
public void ChangeDistance(float dist) {
dist = 1.0f-dist;
float desiredDistance = (dist * (distanceMax - distanceMin)) + distanceMin;
distance = desiredDistance;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612151852im_/https://answers.unity.com/themes/thub/images/avi.jpg)