'Unity - How to spawn hook and reflect to object?
In this game, I'm trying to make a rope with a hook that bounces or reflects the same as the aim direction, so this rope with a hook has max length when reflecting something, basically hook has a tail/rope that is a line renderer that also connected on fire point transform, speaking of reflecting object, for example, this happens when the hook hit the wall, the hook bounce first and then the rope reflects the wall.
PREVIEW
when aim with direction
this is my goal something like this, shoot a hook with a rope that reflects and has the same direction with aim direction
The script
- RaycastReflect
.........
public void GetRotationObject()
{
Vector3 moveVector = (Vector3.up * Player3.instance.newInput_Movement.y - Vector3.left * Player3.instance.newInput_Movement.x);
if (Input.GetAxisRaw("Vertical") != 0 || Input.GetAxisRaw("Horizontal") != 0)
{
transform.rotation = Quaternion.LookRotation(Vector3.forward, moveVector);
}
}
public void ShowRenderer()
{
ray = new Ray2D(transform.position, transform.up);
lineRenderer.positionCount = 1;
lineRenderer.SetPosition(0, transform.position);
float remainingLength = maxLength;
for (int i = 0; i < reflections; i++)
{
hit = Physics2D.Raycast(ray.origin, ray.direction, remainingLength, layerMask);
if (hit)
{
lineRenderer.positionCount += 1;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
remainingLength -= Vector2.Distance(ray.origin, hit.point);
Vector2 updatedDirection = Vector2.Reflect(ray.direction, hit.normal);
ray = new Ray2D(hit.point + updatedDirection * 0.01f, updatedDirection);
if (hit.collider.tag != "Reflect")
break;
}
else
{
lineRenderer.positionCount += 1;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
}
}
}
I have tried to add bounce on hook and spawn it with vector2 UpdateDirection multiply by speed but it's going somewhere, also I think bounce material is not the solution.
another issue is I don't have any idea to connect the rope line renderer to the spawn hook, I've been thinking about distance joint2d but I don't know it's a good idea or not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


