Trouble offsetting an object moved by raycast relative to hit.point
Another question I was unsure how to structure so I apologize. I'm using a particle effect as a ground target for spawning an AOE like attack, like mmorpg's "cast at target location" spells. This target particle effect follows the mouse around along the floor object's normal using a raycast.
I've managed to work out an offset position in the case that the hit.transform is anything other than the floor, so as to avoid the particle effect either passing through walls or climbing them. However I can't figure out how to offset the desired position when the particle effect reaches the edge of the floor and the ray is therefore not hitting anything. This is an issue because if the particle effect reaches the edge of the floor while moving it around (So selecting a target location for an attack) it seems to get stuck, as though it were caught on something and stops following the mouse around. I can free it up only by dragging the mouse in the opposite direction of the floor's edge.
I immediately attempted the same method I used for the instance of trying to move on top of, or through, objects. In that instance I simply lerped between the vector3 hit.point and the current transform.position of the particle effect. However in this instance there is no hit.point and therefore I'm at a complete loss of how to calculate an offset with a desired target position being essentially null. Here's my sorry attempt at it:
else
{
Vector3 hitnulloffset = Vector3.Lerp(playerObject.transform.position, transform.position, 0.1f);
hitnulloffset.y = 0;
transform.position = hitnulloffset;
}
Here's the whole code:
public class VFX_BeamTarget_CursorFollow : MonoBehaviour
{
public GameObject playerObject;
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.transform.CompareTag("Floor"))
{
transform.position = hit.point;
}
else
{
Vector3 offsetpoint = Vector3.Lerp(hit.point, transform.position, 0.25f);
offsetpoint.y = 0;
transform.position = offsetpoint;
}
}
else
{
Vector3 hitnulloffset = Vector3.Lerp(playerObject.transform.position, transform.position, 0.1f);
hitnulloffset.y = 0;
transform.position = hitnulloffset;
}
}
}
Your answer
Follow this Question
Related Questions
Unity Raycasting Issue 1 Answer
Physics Raycast not working 1 Answer
Accessing Raycast collider from another script 0 Answers
Move Gameobject towards/away from position based on the proximity of 2 other objects 0 Answers
I want to make a bot which simulate the maze solver using left hand rule ( wall follower). 0 Answers