**EDIT - Solved, I apparently had two collision masks on my enemy and set the right one on one enemy and the wrong one on another and didn’t even notice. Nothing wrong with the code as far as I can tell… **
Hi
So I am only 6ish months into programming but I am trying to move an enemy when they get hit by the player. During this process I want to move the enemy a set distance over a set amount of time. I don’t have the enemy to tunnel through any terrain.
So what I did was cast a ray from the enemy to the location they are going to end up at. If it hits anything matching the LayerMask of terrain it should go to beside it instead of its full location. This happens before any movement occurs (or it should).
If I hit the enemy into anything larger then 1 unit it works perfectly but if the terrrain is 0.5 units the enemy ends up on the other side. I hope its just something dumb but I can’t see it.
Here is the code:
IEnumerator MoveMe(float duration, Vector2 hitVelocity)
{
float startTime = time.time; //this ties into a plugin for time
float endTime = duration;
Vector2 startingPosition = basicGroundEnemy.transform.position;
float distance = Mathf.Abs((hitVelocity.x > hitVelocity.y) ? hitVelocity.x : hitVelocity.y);
Vector2 position = new Vector2(basicGroundEnemy.transform.position.x + hitVelocity.x, basicGroundEnemy.transform.position.y + hitVelocity.y);
Vector2 direction = hitVelocity.normalized;
float horizontalRayCount = 4f;
float verticalRayCount = 4f;
float horizontalRaySpacing = Mathf.Abs(basicGroundEnemy.transform.localScale.y) / horizontalRayCount;
float verticalRaySpacing = Mathf.Abs(basicGroundEnemy.transform.localScale.x) / verticalRayCount;
Vector2 bottom = new Vector2(0f, (basicGroundEnemy.transform.position.y - (Mathf.Abs(basicGroundEnemy.transform.localScale.y) / 2f)));
Vector2 top = new Vector2(0f, (basicGroundEnemy.transform.position.y + (Mathf.Abs(basicGroundEnemy.transform.localScale.y) / 2f)));
Vector2 right = new Vector2((basicGroundEnemy.transform.position.x + (Mathf.Abs(basicGroundEnemy.transform.localScale.x) / 2f)), 0f);
Vector2 left = new Vector2((basicGroundEnemy.transform.position.x - (Mathf.Abs(basicGroundEnemy.transform.localScale.x) / 2f)), 0f);
int Runs = 0;
while (Runs < ((horizontalRayCount > verticalRayCount) ? horizontalRayCount : verticalRayCount))
{
Vector2 rayOrigin = new Vector2((direction.x > 0) ? right.x : left.x , (bottom.y + 0.15f) + (horizontalRaySpacing * Runs));
RaycastHit2D hitHorizontal2D = Physics2D.Raycast(rayOrigin, direction, distance, collisionMask);
Debug.DrawRay(rayOrigin, direction * distance, Color.green);
if (hitHorizontal2D)
{
if (hitHorizontal2D.point.x > basicGroundEnemy.transform.position.x)
{
position.x = hitHorizontal2D.point.x - ((Mathf.Abs(basicGroundEnemy.transform.localScale.x) / 2f)) - 0.05f;
}
else if (hitHorizontal2D.point.x < basicGroundEnemy.transform.position.x)
{
position.x = hitHorizontal2D.point.x + ((Mathf.Abs(basicGroundEnemy.transform.localScale.x) / 2f)) + 0.05f;
}
}
Vector2 rayVertOrigin = new Vector2((left.x + 0.15f) + (verticalRaySpacing * Runs), (direction.y > 0) ? top.y : bottom.y);
RaycastHit2D hitVertical2D = Physics2D.Raycast(rayVertOrigin, direction, distance, collisionMask);
Debug.DrawRay(rayVertOrigin, direction * distance, Color.green);
if (hitVertical2D)
{
if (hitVertical2D.point.y > basicGroundEnemy.transform.position.y)
{
position.y = hitVertical2D.point.y - ((Mathf.Abs(basicGroundEnemy.transform.localScale.y) / 2f)) - 0.05f;
}
else if (hitVertical2D.point.y < basicGroundEnemy.transform.position.y)
{
position.y = hitVertical2D.point.y + ((Mathf.Abs(basicGroundEnemy.transform.localScale.y) / 2f)) + 0.05f;
}
}
Runs++;
}
while (duration > 0)
{
float percentDone = (time.time - startTime) / endTime;
basicGroundEnemy.transform.position = Vector3.Lerp(basicGroundEnemy.transform.position, position, percentDone);
duration -= time.deltaTime;
yield return null;
}
yield return null;
}