Hello. First time posting here and fairly new to code. I’m working on a game and for one of the camera views I have an point oriented camera that I need to be able to rotate around that point. I need to do a Translate rather than a transform because of how the other coder has the terrain collider set up. The code below in an Update loop. My problem is with translating, it continues to spiral outward in the x and z coordinates. The y system works perfectly. I’m guessing I need to use Slerp or Lerp, but as I’m new to all of this, I need a little help.
Assets/Import package/Scripts Find Standard Assets/Camera Scripts/MouseOrbit. Drop that on your camera. Drop the target object on the target of that script.
If this is non-standard enough that it can’t be a predone orbit script…:
Translate is just a conveniance function to apply a local motion. It looks like you know exactly where you want to be (aimed at centerPos, offset by dX, dY and dZ based on the angles?) In that case, you can just put the camera there directly: Camera.main.transform.position = new Vector(centerPos.x+dx, ...);.
The cos/sins could be replaced by a Quaternion times a vector, which does the math for you. All of the dx/y/z stuff would become something like: Vector3 fromCenter = Quaternion.Euler(xSpin, ySpin, 0)*Vector3.forwards;
Then, so I don’t lose my license, consider using Mathf.Deg2Rad instead of 0.017, or at least const float C1 = 0.017;
I managed to solve the problem. I remade the code entirely using a Lerp function to modify the position if the old position was not the same as the new position and realized that making the camera position a camera rigidbody position change would prevent it from colliding with the terrain. I then turned on extrapolate in the rigidbody menu on the unity inspector. The code is included for anyone who’s looking for it. This allows for both zoom and panning around the center point.
Thanks to everyone who tried to help out!
if (camType==1){
float length = Mathf.Cos(centerAng.y*0.0174532925f)*centerDist;
if ((isLeftKeyDown)&&(isMove=true)){
centerAng.x -= centRotSens;
// Avoid rotation below 0 degrees
while (centerAng.x < 0) {
centerAng.x = 360.0f - Mathf.Abs(centerAng.x);
}
}
if ((isRightKeyDown)&&(isMove=true)) {
centerAng.x += centRotSens;
// Avoid rotation above 360 degrees
while (centerAng.x > 360) {
centerAng.x = 0.0f + (Mathf.Abs(centerAng.x) - 360.0f);
}
}
if ((isUpKeyDown)&&(isMove=true)) {
centerAng.y += centPitSens;
// Avoid pitch above 90 degrees
if (centerAng.y > 90) {
centerAng.y = 90.0f;
}
}
if ((isDownKeyDown)&&(isMove=true)) {
centerAng.y -= centPitSens;
// Avoid pitch below 0 degrees
if (centerAng.y < 0) {
centerAng.y = 0.0f;
}
}
// Control zoom
if (Input.GetAxis("Mouse ScrollWheel")<0) {
centerDist += 20.0f;
if (centerDist > 2000) {
centerDist = 2000.0f;
}
}
if (Input.GetAxis("Mouse ScrollWheel")>0) {
centerDist -= 20.0f;
if (centerDist < 100) {
centerDist = 100.0f;
}
}
if (mouse1IsDown) {
// Listen for mouse x/y, change x/z center position depending on rotation angle
float transX = Input.GetAxis("Mouse X")*15.0f;
float transZ = Input.GetAxis("Mouse Y")*15.0f;
centerPos.x += Mathf.Sin(centerAng.x*0.0174532925f)*transX + Mathf.Cos(centerAng.x*0.0174532925f)*transZ;
centerPos.z += -Mathf.Cos(centerAng.x*0.0174532925f)*transX + Mathf.Sin(centerAng.x*0.0174532925f)*transZ;
}
// Convert centerPos, centerAng, centerDist to Camera position
float posX = centerPos.x+ Mathf.Cos(centerAng.x*0.0174532925f)*length;
float posZ = centerPos.z+ Mathf.Sin(centerAng.x*0.0174532925f)*length;
float posY = Mathf.Sin(centerAng.y*0.0174532925f)*centerDist+centerPos.y;
//Change Camera to a rigid body
Camera.main.transform.rigidbody.position=Vector3.Lerp(Camera.main.transform.position, new Vector3(posX,posY,posZ),60*Time.deltaTime);
Camera.main.transform.LookAt(centerPos);
}