I need to make a ‘Spiderman’ game using Raycast to make my character move with a grappling hook/spiderweb swinging motion, and I need to use a linerenderer to show the spiderweb. With my code below, the linerenderer is shown and hangs on to a building when I press control, but my player still can’t swing. How do I make my player swing from building to building using the linerenderer?
P.S. in my Unity engine, I have a Player game object with Rigidbody, capsule collider, linerenderer, and GrapplingHook & PlayerMovement scripts components. My building are cube prefabs.
Grappling Hook code, attached to Player game object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrapplingHook : MonoBehaviour
{
public Camera playerCamera;
public LineRenderer lineRenderer;
public float maxDistance = 100f;
private Vector3 grapplePoint;
private SpringJoint joint;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetButtonDown("Fire1")) {
StartGrapple();
}
else if (Input.GetButtonUp("Fire1")) {
StopGrapple();
}
}
void StartGrapple()
{
RaycastHit hit;
if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, maxDistance))
{
grapplePoint = hit.point;
joint = gameObject.AddComponent<SpringJoint>();
joint.autoConfigureConnectedAnchor = false;
joint.connectedAnchor = grapplePoint;
float distanceFromPoint = Vector3.Distance(transform.position, grapplePoint);
// The distance grapple will try to keep from grapple point
joint.maxDistance = distanceFromPoint * 0.8f;
joint.minDistance = distanceFromPoint * 0.25f;
joint.spring = 4.5f;
joint.damper = 7f;
joint.massScale = 4.5f;
lineRenderer.positionCount = 2;
}
}
void StopGrapple()
{
lineRenderer.positionCount = 0;
Destroy(joint);
}
void LateUpdate()
{
if (lineRenderer.positionCount > 0)
{
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, grapplePoint);
}
}
void FixedUpdate()
{
if (joint != null)
{
if (Input.GetKey(KeyCode.A))
{
rb.AddForce(-transform.right * 10f); // Add leftward force
}
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(transform.right * 10f); // Add rightward force
}
}
}
}
Player movement code, also attached to Player game object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.W)){
transform.position += transform.forward * Time.deltaTime*5;
}
if(Input.GetKey(KeyCode.S)){
transform.position += -transform.forward * Time.deltaTime;
}
if(Input.GetKey(KeyCode.D)){
transform.position += transform.right * Time.deltaTime;
}
if(Input.GetKey(KeyCode.A)){
transform.position += -transform.right * Time.deltaTime;
}
if(Input.GetKey(KeyCode.Space)){
transform.position += transform.up * 10 * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(Vector3.up, 0.25f);
}
if (Input.GetKey(KeyCode.LeftArrow)){
transform.Rotate(Vector3.down, 0.25f);
}
}
}