Since I am using kinematic rigid body I can’t rely on collisions so I decided to make a custom collision like script that detects how close my player is to the wall. What I want is for my cube to slide against the wall by having lerping to the x y z position disabled depending on the wall’s position but now I have a glitchy lerp script that works, but lerps to the target position in a triangle fashion rather then a straight line
what I want:
what I have:
using UnityEngine;
using System.Collections;
//Make the handles more thick
public class Dash : MonoBehaviour
{
private Vector3 RayCastSpawnPositionOffset;
public GameObject Previouscollided;
private GameObject collided;
public LayerMask Wall;
Vector3 newPosition;
public GameObject Wall1;
public GameObject Wall2;
public GameObject Wall3;
public GameObject Wall4;
public GameObject Floor;
public GameObject Ceiling;
public GameObject RecentWall;
public float speed;
[SerializeField]
private int DistanceOfRayCast;
[SerializeField]
private int CornerDistanceOfRayCast;
public bool UpperCornerView = false;
public bool LowerCornerView = false;
public int CurrentCorner;
private Vector3 WallThicknessOffset;
public int CurrentWall;
private bool LerpToPosition;
Rigidbody rb;
private bool LerpToX = true;[SerializeField]
private bool LerpToY = true;[SerializeField]
private bool LerpToZ = true;[SerializeField]
private Vector3 NewPositionX;
private Vector3 NewPositionZ;
private Vector3 NewPositionY;
void Start()
{
newPosition = transform.position;
CurrentWall = 3;
Rigidbody rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// Debug.Log("CurrentCorner = " + CurrentCorner);
Debug.Log(collided);
if (Input.GetKeyDown("w"))
{
transform.Rotate(1, 0, 0);
}
if (Input.GetKeyDown("s"))
{
transform.Rotate(-1, 0, 0);
}
//This part of the Script handles how the camera handles interactions with corners
RaycastHit HitUp;
Ray UpRay = new Ray(transform.position, Vector3.up * CornerDistanceOfRayCast);
Debug.DrawRay(transform.position, Vector3.up * DistanceOfRayCast, Color.red, 5);
RaycastHit HitDown;
Ray DownRay = new Ray(transform.position, Vector3.down * CornerDistanceOfRayCast);
Debug.DrawRay(transform.position, Vector3.down * DistanceOfRayCast, Color.red, 5);
RaycastHit HitLeft;
Ray LeftRay = new Ray(transform.position, Vector3.left * CornerDistanceOfRayCast);
Debug.DrawRay(transform.position, Vector3.left * CornerDistanceOfRayCast, Color.grey, 5);
RaycastHit HitRight;
Ray RightRay = new Ray(transform.position, Vector3.right * CornerDistanceOfRayCast);
Debug.DrawRay(transform.position, Vector3.right * CornerDistanceOfRayCast, Color.cyan, 5);
RaycastHit HitForward;
Ray ForwardRay = new Ray(transform.position, Vector3.forward * CornerDistanceOfRayCast);
Debug.DrawRay(transform.position, Vector3.forward * CornerDistanceOfRayCast, Color.green, 999);
RaycastHit HitBackward;
Ray BackwardRay = new Ray(transform.position/*newPosition + RayCastSpawnPositionOffset*/, Vector3.back * CornerDistanceOfRayCast);
Debug.DrawRay(transform.position/*newPosition + RayCastSpawnPositionOffset*/, Vector3.back * CornerDistanceOfRayCast, Color.yellow, 999);
if (Physics.Raycast(UpRay, out HitUp, DistanceOfRayCast))
{
UpperCornerView = true;
}
if (Physics.Raycast(DownRay, out HitDown, DistanceOfRayCast))
{
LowerCornerView = true;
}
/*if (Physics.Raycast(ForwardRay, out HitForward, DistanceOfRayCast))
{
if (Vector3.Distance(HitForward.point, transform.position) <= 2)
{
LerpToZ = false;
}
}
if (Physics.Raycast(BackwardRay, out HitBackward, DistanceOfRayCast))
{
if (Vector3.Distance(HitBackward.point, transform.position) <= 2)
{
LerpToZ = false;
}
}
if (Physics.Raycast(RightRay, out HitRight, DistanceOfRayCast))
{
if (Vector3.Distance(HitRight.point, transform.position) <= 2)
{
LerpToX = false;
}
}
if (Physics.Raycast(LeftRay, out HitLeft, DistanceOfRayCast))
{
if (Vector3.Distance(HitLeft.point, transform.position) <= 2)
{
LerpToZ = false;
}
}*/
//Raycast Colors that correspond to their ray
//grey = left
//cyan = right
//green = forward
//yellow = backward
if (Physics.Raycast(LeftRay, out HitLeft, CornerDistanceOfRayCast) && Physics.Raycast(ForwardRay, out HitForward, CornerDistanceOfRayCast))
{
CurrentCorner = 1;
}
if (Physics.Raycast(RightRay, out HitRight, CornerDistanceOfRayCast) && Physics.Raycast(ForwardRay, out HitForward, CornerDistanceOfRayCast))
{
CurrentCorner = 2;
}
if (Physics.Raycast(RightRay, out HitRight, CornerDistanceOfRayCast) && Physics.Raycast(BackwardRay, out HitBackward, CornerDistanceOfRayCast))
{
CurrentCorner = 3;
}
if (Physics.Raycast(LeftRay, out HitLeft, CornerDistanceOfRayCast) && Physics.Raycast(BackwardRay, out HitBackward, CornerDistanceOfRayCast))
{
CurrentCorner = 4;
}
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Wall.value))
{
LerpToX = true;
LerpToZ = true;
LerpToY = true;
CurrentCorner = 0;
UpperCornerView = false;
LowerCornerView = false;
collided = hit.collider.gameObject;
LerpToPosition = true;
RecentWall = collided;
//Debug.Log(collided);
//Cameraposition();
// collided.layer = LayerMask.NameToLayer("Ignore Raycast");
//( ͡° ͜ʖ ͡°)
newPosition = hit.point;
//Debug.Log("test")
Previouscollided.layer = LayerMask.NameToLayer("Wall");
Previouscollided = hit.collider.gameObject;
//Debug.Log(Previousco0llided);
CollidedCheck();
}
}
}
private void CollidedCheck()
{
if (collided == Wall1)
{
//rb.constraints = RigidbodyConstraints.None;
//RayCastSpawnPositionOffset = new Vector3(0, 0, -3);
CurrentWall = 1;
//rb.constraints = RigidbodyConstraints.FreezePositionZ;
//float dist = Vector3.Distance(newPosition, transform.position);
//if(dist <= 2)
//{
//}
StartCoroutine("LerpPos");
// WallThicknessOffset = new Vector3(0, 0, -2);
//transform.position = Vector3.Lerp(transform.position, newPosition + WallThicknessOffset, speed * Time.deltaTime);
}
if (collided == Wall2)
{
//rb.constraints = RigidbodyConstraints.None;
//RayCastSpawnPositionOffset = new Vector3(-3, 0, 0);
CurrentWall = 2;
//rb.constraints = RigidbodyConstraints.FreezePositionX;
StartCoroutine("LerpPos");
}
if (collided == Wall3)
{
//rb.constraints = RigidbodyConstraints.None;
//RayCastSpawnPositionOffset = new Vector3(0, 0, 3);
CurrentWall = 3;
//rb.constraints = RigidbodyConstraints.FreezePositionZ;
StartCoroutine("LerpPos");
}
if (collided == Wall4)
{
//rb.constraints = RigidbodyConstraints.None;
// RayCastSpawnPositionOffset = new Vector3(3, 0, 0);
CurrentWall = 4;
//rb.constraints = RigidbodyConstraints.FreezePositionX;
StartCoroutine("LerpPos");
}
}
private IEnumerator LerpPos()
{
NewPositionX = newPosition;
NewPositionY = newPosition;
NewPositionZ = newPosition;
while (LerpToX == true || LerpToZ == true || LerpToY == true)
{
while (LerpToX == true)
{
NewPositionX.y = 0;
NewPositionX.z = 0;
if (Vector3.Distance(transform.position, NewPositionX) <= 5)
{
LerpToX = false;
yield return null;
}
NewPositionX.y = 0;
NewPositionX.z = 0;
transform.position = Vector3.Lerp(transform.position, NewPositionX, speed);
}
while (LerpToY == true)
{
NewPositionY.x = 0;
NewPositionY.z = 0;
if (Vector3.Distance(transform.position, NewPositionY) <= 5)
{
LerpToY = false;
yield return null;
}
transform.position = Vector3.Lerp(transform.position, NewPositionY, speed);
}
while (LerpToZ == true)
{
NewPositionY.x = 0;
NewPositionY.y = 0;
if (Vector3.Distance(transform.position, NewPositionZ) <= 5)
{
LerpToZ = false;
yield return null;
}
transform.position = Vector3.Lerp(transform.position, NewPositionZ, speed);
}
yield return null;
}
}
}