- Home /
Line Renderer not showing
So I copied Dani's grappling script and modified it since I am using a character controller. It was working pretty well but when I restarted Unity, my Line Renderer has disappeared! I tried re adding the component but nothing worked.
So I investigated, went back to the video but nothing yet. Then when I was grappling below my map, I found out that the line renderer was stuck at the tip of my main canvas. I had 3 or more canvas'(Is that the problem, no it couldn't. It was working just fine with 3 canvas') So I am stuck with that but I will get back to this forum after. Please reply asap!
using System;
using System.Collections;
using UnityEngine;
public class GrapplingGun : MonoBehaviour
{
public LineRenderer lr;
private Vector3 grapplePoint;
public LayerMask whatIsGrappleable;
public Transform gunTip, camera, player;
private float maxDistance = 100f;
private SpringJoint joint;
public CharacterController characterController;
public Rigidbody rb;
public PlayerMovement playerMovement;
public bool isGrappling;
private void Start()
{
PlayerMovement playerMovement = GetComponent<PlayerMovement>();
}
void Awake()
{
lr = GetComponent<LineRenderer>();
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
StartGrapple();
}
else if (Input.GetMouseButtonUp(1))
{
StopGrapple();
}
if (isGrappling)
{
characterController.enabled = false;
}
else characterController.enabled = true;
//Called after Update
void LateUpdate()
{
DrawRope();
}
/// <summary>
/// Call whenever we want to start a grapple
/// </summary>
void StartGrapple()
{
RaycastHit hit;
if (Physics.Raycast(camera.position, camera.forward, out hit, maxDistance, whatIsGrappleable))
{
grapplePoint = hit.point;
joint = player.gameObject.AddComponent<SpringJoint>();
joint.autoConfigureConnectedAnchor = false;
joint.connectedAnchor = grapplePoint;
rb.useGravity = true;
isGrappling = true;
float distanceFromPoint = Vector3.Distance(player.position, grapplePoint);
//The distance grapple will try to keep from grapple point.
joint.maxDistance = distanceFromPoint * 0.8f;
joint.minDistance = distanceFromPoint * 0.25f;
//Adjust these values to fit your game.
joint.spring = 4.5f;
joint.damper = 7f;
joint.massScale = 4.5f;
lr.positionCount = 2;
currentGrapplePosition = gunTip.position;
}
}
/// <summary>
/// Call whenever we want to stop a grapple
/// </summary>
void StopGrapple()
{
lr.positionCount = 0;
Destroy(joint);
rb.useGravity = false;
isGrappling = false;
}
}
private Vector3 currentGrapplePosition;
void DrawRope()
{
//If not grappling, don't draw rope
if (!joint) return;
currentGrapplePosition = Vector3.Lerp(currentGrapplePosition, grapplePoint, Time.deltaTime * 8f);
lr.SetPosition(0, gunTip.position);
lr.SetPosition(1, currentGrapplePosition);
}
public bool IsGrappling()
{
return joint != null;
}
public Vector3 GetGrapplePoint()
{
return grapplePoint;
}
}
Your answer
Follow this Question
Related Questions
How can I fix this error in my Candy Crush style game? 0 Answers
Errors, Parsing Errors everywhere :( please help 2 Answers
Raycast won't fire 3 Answers
cant kill more than one enemy C# 2 Answers
how i -10 from thing in another code 1 Answer