[SOLUTION AT BOTTOM]
in my vr game, i added lightsabers. the collision detection uses raycasts to find an object to slice, or they will once I fix this. the lightsaber can move very fast, since the user can swing their hand. i could manually slow it down, but that would be immersion breaking.
when the user swings the saber very fast, like a flick of the wrist, it may completely miss an object like so:
but i have in mind to interpolate between the last and current positions. this SHOULD work, i dont know why, but it just interpolates position and rotation. im testing it with a trail render for a burnout since its going to have that anyways. this is what it should look like:
but it still looks choppy and there does not appear to be any interpolation. all google searches give me results for fast small things like bullets, not fast thin broad objects like a line. any help would be apreciated!
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Saber : MonoBehaviour
{
public TrailRenderer burnout;
public byte steps = 5;
Vector3 lastPosition = Vector3.zero;
Quaternion lastRotation = Quaternion.identity;
// Update is called once per frame
void Update()
{
Vector3 position = transform.position;
Quaternion rotation = transform.rotation;
RaycastHit burnoutPoint;
for (int i = 0; i < steps; i++)
{
Vector3 currentPosition = Vector3.Lerp(lastPosition, position, i / steps);
Quaternion currentRotation = Quaternion.Lerp(lastRotation, rotation, i / steps);
if (Physics.Raycast(currentPosition, currentRotation * Vector3.up, out burnoutPoint, 1.05f))
{
burnout.transform.position = burnoutPoint.point - (currentRotation * Vector3.up / 50);
burnout.AddPosition(burnoutPoint.point - (currentRotation * Vector3.up / 50));
}
burnout.emitting = Physics.Raycast(currentPosition, currentRotation * Vector3.up, 1.05f);
}
lastPosition = transform.position;
lastRotation = transform.rotation;
}
}
-------EDIT BEFORE UPLOAD!-------
if anyone else has this issue, remember to explicitly convert the iterator to a float or double!
Correct example: |
V
Vector3 currentPosition = Vector3.Lerp(lastPosition, position, (float)i / steps);
Incorrect example, causes integer division, rounding to 1 or 0
Vector3 currentPosition = Vector3.Lerp(lastPosition, position, i / steps);