More specifically a vector2.lerp
, though I need to know how to make it work in general. I’m trying to use a lerp to send out multiple ray casts and it isn’t working correctly.
I’ll post the code involved with the lerp I am trying to use.
RaycastHit2D Raycaster(float rayLength, Vector2 dir, Vector2 origin, float velocity){
RaycastHit2D hitInfo;
float distance = box.height / 2 + (grounded? margin : Mathf.Abs(velocity * Time.deltaTime));
Vector2 direction = velocity > 0? -dir * 20 : dir * 20;
Debug.DrawRay(origin, direction, Color.red);
hitInfo = Physics2D.Raycast(origin, direction, rayLength * 20, 1 << LayerMask.NameToLayer("NormalCollisions"));
return hitInfo;
}
void Gravity(){
bool connected = false;
float lerpAmount = (float)boxCollider.size.x / (float) (verticleRays - 1);//rays -1 because otherwise we dont get to 1.0
Vector2 startPoint = new Vector2((-boxCollider.size.x / 2) + transform.position.x, (-boxCollider.size.y / 2) + transform.position.y);//these are used to position the raycasts on the horizontal axis
Vector2 endPoint = new Vector2((boxCollider.size.x / 2) + transform.position.x, (-boxCollider.size.y / 2) + transform.position.y);
Vector2 origin = Vector2.Lerp(startPoint, endPoint, lerpAmount * Time.deltaTime);
//an elegent way to apply gravity. subtract from y speed with terminal velocity = maxFall
velocity = new Vector2(velocity.x, Mathf.Max(velocity.y = gravity, -maxFall));
float newVelocityY = velocity.y;
float rayLength = box.height / 2 + Mathf.Abs (newVelocityY * Time.deltaTime);
for(int i = 0; i < verticleRays; i ++){
print ("BoxSize: " + boxCollider.size.x + " LerpAmount: " + lerpAmount + " lerpAmount * time: " + (lerpAmount * Time.time) +
" lerpamount * deltatime: " + (lerpAmount * Time.deltaTime) + " origin: " + origin + " startpoint: " + startPoint +
" endpoint: " + endPoint + " boxSize: " + boxCollider.size.x + " time.deltatime: " + Time.deltaTime +
" time.time: " + Time.time);
RaycastHit2D hitInfo = Raycaster(rayLength, Vector2.up, origin, newVelocityY);
connected = hitInfo;
print ("Ray fraction " + hitInfo.fraction);
if(!connected){
transform.Translate(-Vector2.up * (gravity * Time.deltaTime));
velocity = new Vector2(velocity.x, 0);
}
if(connected){
if(hitInfo.fraction > 0){
transform.Translate(-Vector2.up * (gravity * Time.deltaTime));
velocity = new Vector2(velocity.x, 0);
print ("Gravity activated");
break;
}
}
}
}
Now the trouble I’m having is that its not ‘lerping’ when I use time.deltatime
, it literally just sends all my raycasts from the start point. If I change the lerpamount
sometimes it will make one interpolation. It moves if I use time.time
but it moves in much smaller units than the lerpamount
and I dont wont to use time.time
for obvious reasons.
This is essentially the section of code (Its in the code above but I’m putting it here for clarity) that I’ve been working with to edit the variables in my vector2.lerp
.
float lerpAmount = (float)boxCollider.size.x / (float) (verticleRays - 1);//rays -1 because otherwise we dont get to 1.0
Vector2 startPoint = new Vector2((-boxCollider.size.x / 2) + transform.position.x, (-boxCollider.size.y / 2) + transform.position.y);//these are used to position the raycasts on the horizontal axis
Vector2 endPoint = new Vector2((boxCollider.size.x / 2) + transform.position.x, (-boxCollider.size.y / 2) + transform.position.y);
Vector2 origin = Vector2.Lerp(startPoint, endPoint, lerpAmount * Time.deltaTime);
The vector2 origin
heading is the main one that I am editing and its where ‘I’ think most of the issues are. I could however be wrong, and the problem could be anywhere. My next guess would be lerpAmount
or where I call Raycaster
inside the for loop.
I read the description of lerp in the unity docs, I’ve read a wikipedia page on it, I’m really pretty sure I understand what its supposed to be doing. I give it a start point and an end point, and an amount to move by then it cycles from one end to the other moving the lerpAmount
with each cycle.
I’ve read that its commonly used with time.deltaTime
but for some reason thats not working for me. I also would expect it to start fresh each time the function it exists in is called but for some reason (maybe I need to make it static?) thats not working either. With all that in mind if someone could fill me in on what I’m doing wrong using this lerp function that would be great.
Thanks in advance for any help that is given.