2D character controller rework for Unity. The code is rough, but I can position the rays where I want and give each different lengths.
Code:
[Serializable]
public class Ray2DPropertyList
{
//public CollisionSides
//[List]
public List<Ray2DProperties> nestedRay2DProperties;
}
[RequireComponent(typeof(Rigidbody2D))]
public class CharacterController2D : MonoBehaviour {
public Vector3 velocity;
public Vector2 rayOffset;
public LayerMask platformLayerMasks;
[SerializeField]
public Ray2DProperties rayProp;
public Ray2DProperties rayProp2;
// [SerializeField]
//public List<Ray2DPropertyList> rayProperties;
public Ray2DPropertyList feet;
public Ray2DPropertyList head;
public Ray2DPropertyList sides;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
UpdateControl();
velocity *=Time.deltaTime * 3;
//CastARay();
HorizontalCollisionCheck(ref velocity);
VerticalCollisionCheck(ref velocity);
// Debug.Log(velocity.x);
transform.Translate(velocity);
}
void DrawDebugLine(Ray2D r)
{
Debug.DrawLine(r.origin,new Vector3(1 + r.origin.x, r.origin.y,0));
//Gizmos.DrawLine(r.origin,new Vector3(1 + r.origin.x, r.origin.y,0));
}
void VerticalCollisionCheck(ref Vector3 rawVelocity)
{
int directionCheck = (rawVelocity.y<0)?-1:1;
Vector2 rayPosition = new Vector2(transform.position.x,
transform.position.y);
if (directionCheck<0)
{
int rayCount = feet.nestedRay2DProperties.Count;
for (int i = 0; i < rayCount; i++)
{
Vector2 tempPos = rayPosition;
tempPos.x += feet.nestedRay2DProperties[i].rayPositionOffset.x;
tempPos.y += feet.nestedRay2DProperties[i].rayPositionOffset.y;
Ray2D r = new Ray2D(tempPos, Vector2.up * directionCheck);
RaycastHit2D hit = Physics2D.Raycast(r.origin, r.direction,
Mathf.Abs(rawVelocity.y) + feet.nestedRay2DProperties[i].raylength,
platformLayerMasks);
if (hit)
{
rawVelocity.y =(hit.point.y-r.origin.y)+ feet.nestedRay2DProperties[i].raylength;
return;
}
}
}
if (directionCheck>0)
{
int rayCount = head.nestedRay2DProperties.Count;
for (int i = 0; i < rayCount; i++)
{
Vector2 tempPos = rayPosition;
tempPos.x += head.nestedRay2DProperties[i].rayPositionOffset.x;
tempPos.y += head.nestedRay2DProperties[i].rayPositionOffset.y;
Ray2D r = new Ray2D(tempPos, Vector2.up * directionCheck);
RaycastHit2D hit = Physics2D.Raycast(r.origin, r.direction,
Mathf.Abs(rawVelocity.y) + head.nestedRay2DProperties[i].raylength,
platformLayerMasks);
if (hit)
{
rawVelocity.y =(hit.point.y-r.origin.y)- head.nestedRay2DProperties[i].raylength;
return;
}
}
}
}
void HorizontalCollisionCheck(ref Vector3 rawVelocity)
{
int directionCheck = (rawVelocity.x<0)?-1:1;
Vector2 rayPosition = new Vector2(transform.position.x,
transform.position.y);
int rayCount = sides.nestedRay2DProperties.Count;
// Debug.Log(sidesCount);
for (int i = 0; i < rayCount; i++)
{
// Vector2 tempPos = rayPosition;
// if (directionCheck>0) {
// tempPos.x += sides.nestedRay2DProperties[i].rayPositionOffset.x;
// tempPos.y += sides.nestedRay2DProperties[i].rayPositionOffset.y;
// }
// if (directionCheck<0) {
// tempPos.x -= sides.nestedRay2DProperties[i].rayPositionOffset.x;
// tempPos.y += sides.nestedRay2DProperties[i].rayPositionOffset.y;
// }
// tempPos.x += sides.nestedRay2DProperties[i].rayPositionOffset.x * directionCheck;
// tempPos.y += sides.nestedRay2DProperties[i].rayPositionOffset.y;
Vector2 tempPos =
new Vector2(rayPosition.x +
sides.nestedRay2DProperties[i].rayPositionOffset.x,
rayPosition.y + sides.nestedRay2DProperties[i].rayPositionOffset.y);
//tempPos.x=tempPos.x*directionCheck;
Ray2D r = new Ray2D(tempPos, Vector2.right * directionCheck);
RaycastHit2D hit = Physics2D.Raycast(r.origin, r.direction,
Mathf.Abs(rawVelocity.x) + sides.nestedRay2DProperties[i].raylength,
platformLayerMasks);
if (hit)
{
// Debug.Log(rawVelocity);
if (directionCheck<0)
{
//rawVelocity.x =(hit.point.x-r.origin.x)-(-sides.nestedRay2DProperties[i].raylength);
rawVelocity.x =(hit.point.x-r.origin.x)-(sides.nestedRay2DProperties[i].raylength*directionCheck);
//rawVelocity.x = 0;
}
if (directionCheck>0)
{
rawVelocity.x =(hit.point.x-r.origin.x)-sides.nestedRay2DProperties[i].raylength;
//rawVelocity.x = 0;
}
// Debug.Log(hit.point);
// Debug.Log(tempPos);
//Debug.Log(r.origin);
//Debug.Log(rawVelocity.x);
break;
}
}
}
void CastARay()
{
//Vector2 temp = new Vector2(transform.position.x + velocity.x, transform.position.y + velocity.y);
Vector2 temp = new Vector2(transform.position.x, transform.position.y);
temp+=rayProp.rayPositionOffset;
//temp+=rayOffset;
Ray2D r = new Ray2D(temp, Vector2.right);
//DrawDebugLine(r);
RaycastHit2D hit = Physics2D.Raycast(r.origin, r.direction, velocity.x + rayProp.raylength, platformLayerMasks);
//Debug.Log(r.origin.x + 1);
if (hit) {
// Debug.Log("It Hit!");
// Debug.Log(hit.point);
if(velocity.x>0)
velocity.x =(hit.point.x -r.origin.x)-rayProp.raylength;// * Time.deltaTime;
//transform.Translate(new Vector3(-(r.origin.x +1 - hit.point.x),0,0));
}
temp = new Vector2(transform.position.x, transform.position.y);
temp+=rayProp2.rayPositionOffset;
r = new Ray2D(temp, Vector2.right);
hit = Physics2D.Raycast(r.origin, r.direction * -1, velocity.x + rayProp2.raylength, platformLayerMasks);
if (hit) {
// Debug.Log("It Hit!");
// Debug.Log(hit.point);
if(velocity.x<0)
velocity.x =(hit.point.x -r.origin.x)+rayProp2.raylength;// * Time.deltaTime;
//transform.Translate(new Vector3(-(r.origin.x +1 - hit.point.x),0,0));
}
}
void UpdateControl()
{
if (Input.GetKey(KeyCode.A))
{
velocity.x = -1;
}
else if (Input.GetKey(KeyCode.D))
{
velocity.x = 1;
}
else
{
//velocity.x = 0;
}
if (Input.GetKey(KeyCode.S))
{
velocity.y = -1;
}
else if (Input.GetKey(KeyCode.W))
{
velocity.y = 1;
}
else
{
velocity.y = 0;
}
}
void OnDrawGizmos()
{
Vector2 temp;
// Vector2 temp = new Vector2(transform.position.x+rayProp.rayPositionOffset.x,
// transform.position.y+rayProp.rayPositionOffset.y);//transform.position + rayProp.rayPositionOffset;
// Gizmos.DrawLine(temp,new Vector3(rayProp.raylength + temp.x, temp.y,0));
int max = sides.nestedRay2DProperties.Count;
for (int i = 0; i < max; i++)
{
temp = new Vector2(transform.position.x+sides.nestedRay2DProperties[i].rayPositionOffset.x,
transform.position.y+sides.nestedRay2DProperties[i].rayPositionOffset.y);
Gizmos.DrawLine(temp,new Vector3(sides.nestedRay2DProperties[i].raylength + temp.x, temp.y,transform.position.z));
}
max = feet.nestedRay2DProperties.Count;
for (int i = 0; i < max; i++)
{
temp = new Vector2(transform.position.x+feet.nestedRay2DProperties[i].rayPositionOffset.x,
transform.position.y+feet.nestedRay2DProperties[i].rayPositionOffset.y);
Gizmos.DrawLine(temp,new Vector3(temp.x,temp.y - feet.nestedRay2DProperties[i].raylength ,transform.position.z));
}
max = head.nestedRay2DProperties.Count;
for (int i = 0; i < max; i++)
{
temp = new Vector2(transform.position.x+head.nestedRay2DProperties[i].rayPositionOffset.x,
transform.position.y+head.nestedRay2DProperties[i].rayPositionOffset.y);
Gizmos.DrawLine(temp,new Vector3(temp.x,head.nestedRay2DProperties[i].raylength + temp.y,transform.position.z));
}
}
}