Last active
August 5, 2017 15:48
-
-
Save mmj-the-fighter/7cffa7845fcac1d866f44b41c46356e7 to your computer and use it in GitHub Desktop.
A method to slide camera along an obstacle in a 3D world.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copyright © 2017 Manoj M J | |
//All Rights Reserved | |
/* | |
A method to slide camera along an obstacle. | |
Note: This script uses Unity3D game engine api and libraries. | |
*/ | |
void SlideCameraOnCollision( | |
Vector3 obstacleNormal, | |
Vector3 cameraForward, | |
float forwardInputMagnitude, | |
Transform cameraTransform, | |
float slidingSpeed, | |
float movementSmoothening | |
) | |
{ | |
Vector3 side = | |
Vector3.Cross(cameraForward, obstacleNormal); | |
Vector3 tangent = | |
(Vector3.Cross(side, obstacleNormal)).normalized; | |
cameraTransform.position = | |
Vector3.Lerp(cameraTransform.position, | |
cameraTransform.position - | |
tangent * (forwardInputMagnitude * slidingSpeed), | |
movementSmoothening * Time.deltaTime); | |
} | |
//Driver code | |
void Update() | |
{ | |
//... | |
SlideCameraOnCollision(raycasthitInfo.normal, | |
fwd, | |
Input.GetAxis("Vertical"), | |
CameraTransform, | |
SlidingSpeed, | |
MovementSmoothening); | |
//... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment