Last active
October 1, 2015 12:05
-
-
Save romainPechot/9539274e14e8aeeb7b14 to your computer and use it in GitHub Desktop.
simple paddle bounce controller for arkanoid like game
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
using UnityEngine; | |
using System.Collections; | |
public class PaddleBounceDirectionController : MonoBehaviour | |
{ | |
[SerializeField] | |
private Transform bounceSurface; | |
[SerializeField] | |
private float bounceSurfaceLength = 2f; | |
[SerializeField] | |
private float borderAngle = 45f; | |
[SerializeField] | |
private AnimationCurve curvePaddle = AnimationCurve.Linear(0f, 0f, 1f, 1f); | |
public bool debugGizmoSurface = true; | |
public bool debugGizmoExample = true; | |
public float bouncePositionExample = 0f; | |
private Vector3 result = Vector3.zero; | |
private float angleResult = 0f; | |
public Vector3 GetBounceDirection(Vector3 worldCollisionPoint) | |
{ | |
if(bounceSurface != null) | |
{ | |
// world to local | |
worldCollisionPoint = bounceSurface.InverseTransformPoint(worldCollisionPoint); | |
angleResult = ComputeAngle(worldCollisionPoint.x); | |
result.x = Mathf.Sin(angleResult * Mathf.Deg2Rad); | |
result.z = Mathf.Cos(angleResult * Mathf.Deg2Rad); | |
// local to world space | |
result = bounceSurface.TransformDirection(result); | |
// normalize | |
result.Normalize(); | |
return result; | |
} | |
else | |
{ | |
Debug.LogWarning("Missing transform ref !", this); | |
return Vector3.zero; | |
} | |
}// GetBounceDirection() | |
public float ComputeAngle(float localSurfacePosition) | |
{ | |
float ratioPosition = localSurfacePosition * 2f / bounceSurfaceLength; | |
ratioPosition = curvePaddle.Evaluate(Mathf.Abs(ratioPosition)) * Mathf.Sign(ratioPosition); | |
return ratioPosition * borderAngle; | |
}// ComputeAngle() | |
private void OnValidate() | |
{ | |
// clamp example | |
bouncePositionExample = Mathf.Clamp(bouncePositionExample, -bounceSurfaceLength * 0.5f, bounceSurfaceLength * 0.5f); | |
}// OnValidate() | |
private void OnDrawGizmos() | |
{ | |
if(bounceSurface == null) return; | |
Gizmos.color = Color.cyan; | |
Vector3 surfaceLeftPoint = bounceSurface.TransformPoint(Vector3.left * bounceSurfaceLength * 0.5f); | |
Vector3 surfaceRightPoint = bounceSurface.TransformPoint(Vector3.right * bounceSurfaceLength * 0.5f); | |
if(debugGizmoSurface) | |
{ | |
Gizmos.DrawLine(surfaceLeftPoint, surfaceRightPoint); | |
} | |
if(debugGizmoExample) | |
{ | |
Vector3 exampleBounceStartPoint = bounceSurface.TransformPoint(Vector3.right * bouncePositionExample); | |
Vector3 exampleBounceDirection = GetBounceDirection(exampleBounceStartPoint); | |
Gizmos.DrawRay(exampleBounceStartPoint, exampleBounceDirection); | |
} | |
}// OnDrawGizmos() | |
}// PaddleBounceDirectionController : MonoBehaviour |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment