Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vector3 gravityUp = myPlayerGO.transform.position - new Vector3(0,0,0);
- gravityUp.Normalize();
- myRigidbody.AddForce(gravityUp * Gravity * myRigidbody.mass);
- if(Grounded >= 1)
- myRigidbody.drag = 0.1f;
- else
- myRigidbody.drag = 1.0f;
- Quaternion quatern = Quaternion.FromToRotation(myPlayerGO.transform.up, gravityUp);
- quatern = quatern * myPlayerGO.transform.rotation;
- myPlayerGO.transform.rotation = Quaternion.Slerp(myPlayerGO.transform.rotation, quatern, 0.1f);
- if(myRigidbody.velocity.magnitude < MoveSpeed() && Grounded >= 1)
- {
- myRigidbody.AddForce(myPlayerGO.transform.forward * VAxis * MoveSpeed());
- myRigidbody.AddForce(myPlayerGO.transform.right * HAxis * MoveSpeed());
- }
- if(Jumping == true && Grounded >= 1)
- {
- myRigidbody.velocity = myRigidbody.velocity + (myRigidbody.transform.up * JumpSpeed);
- Jumping = false;
- }
- if(PlayerIsCombative || (!PlayerIsCombative && MouseButton2))
- {
- MouseX += MouseXAxis * 5f;
- MouseY -= MouseYAxis * 5f;
- if (MouseY < -360) MouseY += 360;
- if (MouseY > 360) MouseY -= 360;
- //MouseY = Mathf.Clamp(MouseY, -40f, 80f);
- }
- if(MouseScrollAxis < -0.01f || MouseScrollAxis > 0.01f)
- {
- desiredDistance = Mathf.Clamp(Distance - MouseScrollAxis * 5f,0.5f,5f);
- preOccludedDistance = desiredDistance;
- distanceSmooth = 0.05f;
- }
- int count = 0;
- do{
- if(desiredDistance < preOccludedDistance)
- {
- Vector3 pos = CalculatePosition(MouseY, MouseX, preOccludedDistance);
- float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, pos);
- if(nearestDistance == -1 || nearestDistance > preOccludedDistance)
- desiredDistance = preOccludedDistance;
- }
- Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, distanceSmooth);
- desiredPosition = CalculatePosition(MouseY, MouseX, Distance);
- count++;
- }while(CheckIfOccluded(count));
- CheckCameraPoints(CameraLookAt.transform.localPosition, desiredPosition);
- float posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, 0.05f);
- float posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, 0.1f);
- float posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, 0.05f);
- position = new Vector3(posX, posY, posZ);
- myPlayerCameraGO.transform.localPosition = position;
- Vector3 relPlayerPosition = CameraLookAt.transform.position - myPlayerCameraGO.transform.position;
- myPlayerCameraGO.transform.rotation = Quaternion.LookRotation(relPlayerPosition, myPlayerGO.transform.up);
- //Vector3 v = myPlayerCameraGO.transform.forward;
- //Quaternion q = Quaternion.LookRotation(v, myPlayerGO.transform.up);
- //q.eulerAngles = new Vector3(0,q.eulerAngles.y,0);
- //myPlayerGO.transform.rotation = Quaternion.LookRotation(myPlayerCameraGO.transform.forward, myPlayerGO.transform.up);
- //myPlayerGO.transform.eulerAngles = new Vector3(0,myPlayerCameraGO.transform.eulerAngles.y,0);
- //myPlayerGO.transform.forward = myPlayerCameraGO.transform.TransformDirection(myPlayerCameraGO.transform.forward);
- ////////////////////////////////////////////////////////////////////////////
- private Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
- {
- Vector3 direction = new Vector3(0, 0, -distance);
- Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
- return CameraLookAt.transform.localPosition + rotation * direction;
- }
- private bool CheckIfOccluded(int count)
- {
- bool isOccluded = false;
- float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, desiredPosition);
- if(nearestDistance != -1)
- {
- if(count < 10)
- {
- isOccluded = true;
- Distance -= 0.05f;
- if(Distance < 0.25f)
- Distance = 0.25f;
- }
- else
- Distance = nearestDistance - myPlayerCamera.nearClipPlane;
- desiredDistance = Distance;
- distanceSmooth = 1f;
- }
- return isOccluded;
- }
- private float CheckCameraPoints(Vector3 from, Vector3 to)
- {
- var nearDistance = -1f;
- RaycastHit hitInfo;
- ClipPlanePoints clipPlanePoints = ClipPlaneAtNear( to);
- if(Physics.Linecast(from, clipPlanePoints.UpperLeft, out hitInfo) && hitInfo.collider.tag != "Player")
- nearDistance = hitInfo.distance;
- if(Physics.Linecast(from, clipPlanePoints.LowerLeft, out hitInfo) && hitInfo.collider.tag != "Player")
- if(hitInfo.distance < nearDistance || nearDistance == -1)
- nearDistance = hitInfo.distance;
- if(Physics.Linecast(from, clipPlanePoints.UpperRight, out hitInfo) && hitInfo.collider.tag != "Player")
- if(hitInfo.distance < nearDistance || nearDistance == -1)
- nearDistance = hitInfo.distance;
- if(Physics.Linecast(from, clipPlanePoints.LowerRight, out hitInfo) && hitInfo.collider.tag != "Player")
- if(hitInfo.distance < nearDistance || nearDistance == -1)
- nearDistance = hitInfo.distance;
- if(Physics.Linecast(from, to + transform.forward * -myPlayerCamera.nearClipPlane, out hitInfo) && hitInfo.collider.tag != "Player")
- if(hitInfo.distance < nearDistance || nearDistance == -1)
- nearDistance = hitInfo.distance;
- return nearDistance;
- }
- private static ClipPlanePoints ClipPlaneAtNear(Vector3 pos)
- {
- ClipPlanePoints clipPlanePoints = new ClipPlanePoints();
- Transform transform = myPlayerCameraGO.transform;
- float HalfFOV = (myPlayerCamera.fieldOfView / 2) * Mathf.Deg2Rad;
- float aspect = myPlayerCamera.aspect;
- float distance = myPlayerCamera.nearClipPlane;
- float height = distance * Mathf.Tan(HalfFOV);
- float width = height * aspect;
- clipPlanePoints.LowerRight = pos + transform.right * width;
- clipPlanePoints.LowerRight -= transform.up * height;
- clipPlanePoints.LowerRight += transform.forward * distance;
- clipPlanePoints.LowerLeft = pos - transform.right * width;
- clipPlanePoints.LowerLeft -= transform.up * height;
- clipPlanePoints.LowerLeft += transform.forward * distance;
- clipPlanePoints.UpperRight = pos + transform.right * width;
- clipPlanePoints.UpperRight += transform.up * height;
- clipPlanePoints.UpperRight += transform.forward * distance;
- clipPlanePoints.UpperLeft = pos - transform.right * width;
- clipPlanePoints.UpperLeft += transform.up * height;
- clipPlanePoints.UpperLeft += transform.forward * distance;
- return clipPlanePoints;
- }
- private struct ClipPlanePoints
- {
- public Vector3 UpperLeft;
- public Vector3 LowerLeft;
- public Vector3 UpperRight;
- public Vector3 LowerRight;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement