Hello i'm doing a simple 3d game and wanted to make something like a grappling hook and it all works perfectly, but i can't find a solution how to make it to have a texture. Here's my code:
SpringJoint joint;
public GameObject playerCam;
Vector3 grapplepoint;
private void StartGrapplinghook()
{
print("grapple");
RaycastHit[] hits = Physics.RaycastAll(playerCam.transform.position, playerCam.transform.forward, 30f);
if (hits.Length < 1) return;
grapplepoint = hits[0].point;
joint = gameObject.AddComponent<SpringJoint>();
joint.autoConfigureConnectedAnchor = false;
joint.connectedAnchor = grapplepoint;
joint.spring = 2.5f;
joint.damper = 0.25f;
}
Hope you guys can help me.
I would use a LineRenderer
for this, and update its positions every frame if it's enabled.
Make and configure a LineRenderer
component on the same gameobject the script is on. Explanation for code in comments.
[RequireComponent(typeof(LineRenderer))]
public class MyClassName : MonoBehaviour
{
[SerializeField]
LineRenderer grappleRenderer;
SpringJoint joint;
public GameObject playerCam;
Vector3 grapplepoint;
void Awake()
{
// Whatever is already here...
// If you don't need a fresh grapple copy every time,
// create them once here or in the editor and re-use them
joint = gameObject.AddComponent<SpringJoint>();
joint.autoConfigureConnectedAnchor = false;
joint.spring = 2.5f;
joint.damper = 0.25f;
grappleRenderer = GetComponent<LineRenderer>();
grappleRenderer.positionCount = 2; // this could just be set in the inspector
// Don't use the grapple by default
joint.enabled = false;
grappleRenderer.enabled = false;
}
private void StartGrapplinghook()
{
print("grapple");
RaycastHit[] hits = Physics.RaycastAll(playerCam.transform.position,
playerCam.transform.forward, 30f);
if (hits.Length < 1) return;
grapplepoint = hits[0].point;
joint.enabled = true;
joint.connectedAnchor = grapplepoint;
grappleRenderer.enabled = true;
}
void StopGrapplingHook()
{
// turn off joint and renderer
joint.enabled = false;
grappleRenderer.enabled = false;
}
// grapple rendering can happen after all other updates finish
// determining if it should be rendered or not that frame
void LateUpdate()
{
if (grappleRenderer.enabled)
{
Vector3 grappleStart = transform.position; // or something that makes sense
grappleRenderer.SetPoints(new Vector3[]{grapplepoint, grappleStart});
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.