private void MoveWithTerrain(float angle) { angle = angle * Mathf.Deg2Rad; Vector3 currentPosition = centerBottom.position; float newX = (Mathf.Cos(angle) * calculateSpeed()) + currentPosition.x; float newZ = (Mathf.Sin(angle) * calculateSpeed()) + currentPosition.z; float newY = EnvironmentPhysics.FindHeightAt(newX, newZ); Vector3 newPosition = new Vector3(newX, newY, newZ); direction.Face(newPosition); if (EnvironmentPhysics.WalkableAt(newPosition.x, newPosition.z, 1)) { float twoDDistance = FastMath.Hyp(currentPosition.x , currentPosition.z , newPosition.x , newPosition.z); Vector3 hypotenuse = newPosition - currentPosition; Vector3 unitHypotenuse = hypotenuse.normalized; unitHypotenuse.Scale(FastMath.CreateVectorCube(twoDDistance)); Vector3 finalPosition = unitHypotenuse + currentPosition; centerBottom.position = new Vector3(finalPosition.x , EnvironmentPhysics.FindWalkableHeightAt(finalPosition.x , finalPosition.z) , finalPosition.z ); } /* * centerBottom is implied to be the child of * the gameobject movementcontroller is attached to */ Transform centerBottomParent = centerBottom.parent; centerBottom.parent = null; body.position = centerBottom.position + displacementFromCenterBottom; centerBottom.parent = centerBottomParent; }
private void ApplyMouseOffset() { Vector3 focusScreenPos = cam.WorldToScreenPoint(focus.position); Vector2 vectorToMouse = mousePosition - focusScreenPos; float vectorToMouseLength = vectorToMouse.magnitude; float vectorToMouseAngle = Mathf.Atan2( vectorToMouse.y, vectorToMouse.x ); Vector3 camRight = camIndependentLocation.transform.right; Vector3 camForward = camIndependentLocation.transform.forward; Vector3 screenVectorToMouse = Quaternion.AngleAxis(Mathf.Rad2Deg * vectorToMouseAngle, camForward) * camRight; Vector3 nScreenVectorToMouse = screenVectorToMouse.normalized; float screenMouseOffset = vectorToMouseLength * distanceFromFocus / 1000; nScreenVectorToMouse.Scale(FastMath.CreateVectorCube(screenMouseOffset)); camIndependentLocation.position += nScreenVectorToMouse; }