Reputation: 99
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if( selectedObjs.Count==0&&Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray,out hit,100, 1 << 9))
{
currentPos = hit.point;
}
}
if (mouseClickMode !=3 && mouseClickMode != 4 && !uiMenu_on
&& Input.GetMouseButton(0))
{
float rotX = Input.GetAxis("Mouse X");
float rotY = -Input.GetAxis("Mouse Y");
if (selectedObjs.Count > 0)
currentPos = objectManager.ReturnPos(selectedObjs[0]);
transform.RotateAround(currentPos, Vector3.up, Time.deltaTime *450* rotX);
transform.RotateAround(currentPos, Vector3.right, Time.deltaTime*450 * rotY);
}
This rotation is rotating on the x axis and then on the y axis.
But when the mouse is moved diagonally, the axis is not a diagonal one. How can I have the object rotate around point on a diagonal axis when the mouse is moved diagonally?
Upvotes: 5
Views: 648
Reputation: 20249
Find the mouse's direction in world space using the camera's transform.TransformDirection
. (note NO negative on float rotY = Input.GetAxis("Mouse Y");
)
Use cross product between the mouse's world direction and the camera's forward to determine the axis you're interested in rotating around.
Rotate based on the magnitude of the mouse's movement.
Altogether:
[RequireComponent(typeof(Collider))]
public class TestScript : MonoBehaviour
{
Vector3 currentPos;
Camera mainCam;
private void Start()
{
mainCam = Camera.main;
}
private void Update()
{
RaycastHit hit;
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit))
{
currentPos = hit.point;
}
}
if (Input.GetMouseButton(0))
{
float rotX = Input.GetAxis("Mouse X");
float rotY = Input.GetAxis("Mouse Y");
Vector3 mouseMove = new Vector3(rotX, rotY, 0f);
Vector3 mouseWorldDirection = mainCam.transform.TransformDirection(
mouseMove);
Vector3 rotAxis = Vector3.Cross(mouseWorldDirection,
mainCam.transform.forward);
transform.RotateAround(currentPos, rotAxis, Time.deltaTime * 450f
* mouseMove.magnitude);
}
}
}
In your code, it might look like this:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if( selectedObjs.Count==0&&Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray,out hit,100, 1 << 9))
{
currentPos = hit.point;
}
}
if (mouseClickMode !=3 && mouseClickMode != 4 && !uiMenu_on
&& Input.GetMouseButton(0))
{
float rotX = Input.GetAxis("Mouse X");
float rotY = Input.GetAxis("Mouse Y");
if (selectedObjs.Count > 0)
currentPos = objectManager.ReturnPos(selectedObjs[0]);
Vector3 mouseMove = new Vector3(rotX, rotY, 0f);
Vector3 mouseWorldDirection = mainCam.transform.TransformDirection(mouseMove);
Vector3 rotAxis = Vector3.Cross(mouseWorldDirection, mainCam.transform.forward);
transform.RotateAround(currentPos, rotAxis, Time.deltaTime * 450f
* mouseMove.magnitude);
}
Upvotes: 2
Reputation: 414
Instead of:
transform.RotateAround(currentPos, Vector3.up, Time.deltaTime *450* rotX);
transform.RotateAround(currentPos, Vector3.right, Time.deltaTime*450 * rotY);
Try:
transform.RotateAround(currentPos, new Vector3(rotY, rotX, 0), Time.deltaTime * 450);
So that you're not rotating on the 2 axis seperately.
Or if don't want only diagonal movement:
float diagonalDeadzone = 0.2f;
float rotX = Input.GetAxis("Mouse X");
float rotY = -Input.GetAxis("Mouse Y");
if (selectedObjs.Count > 0)
currentPos = objectManager.ReturnPos(selectedObjs[0]);
if (rotX < diagonalDeadzone && rotX > -diagonalDeadzone)
{
transform.RotateAround(currentPos, Vector3.up * rotX, Time.deltaTime * 450);
}
else if (rotY < diagonalDeadzone && rotY > -diagonalDeadzone)
{
transform.RotateAround(currentPos, Vector3.right * rotY, Time.deltaTime * 450);
}
Upvotes: 0