Skip to content

Instantly share code, notes, and snippets.

@mmj-the-fighter
Last active August 5, 2017 15:48
Show Gist options
  • Save mmj-the-fighter/7cffa7845fcac1d866f44b41c46356e7 to your computer and use it in GitHub Desktop.
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.
//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