My game has 2 grapplers, one for left and right, and for some reason, and the line renderer won’t ever actually attach to the game object. It’s always floating a little bit to the side of the gameobject, is there any way to fix this?
Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
public class Grapple : MonoBehaviour
{
[Header("Keybinds")]
public string direction = "";
public InputActionReference attackKeybind;
[Header("Grappling")]
public float maxDistance = 100f;
public bool grappling = false;
public Transform grappleOrigin;
public LayerMask whatIsGrappleable;
public GameObject player;
public GameObject GrapplePoint;
public float grappleForce = 38f;
private LineRenderer lr;
private Vector3 grapplePoint;
private Vector3 currentGrapplePosition;
// public AudioClip grappleSound;
// public AudioClip hitSound;
[Header("Camera")]
public Transform playerCam;
[Header("Other")]
Animator animator;
public const string IDLE = "Idle";
public const string GRAPPLE = "Grapple";
public const string IDLER = "IdleR";
public const string GRAPPLER = "GrappleR";
string currentAnimationState;
private void OnEnable()
{
attackKeybind.action.Enable();
}
private void OnDisable()
{
attackKeybind.action.Disable();
}
void Awake()
{
animator = GetComponentInChildren<Animator>();
lr = GetComponent<LineRenderer>();
lr.positionCount = 0;
}
// Update is called once per frame
void Update()
{
if (attackKeybind.action.IsPressed())
{
RaycastHit hit;
if (Physics.Raycast(playerCam.position, playerCam.forward, out hit, maxDistance, whatIsGrappleable))
{
if (!grappling)
{
GameObject copyGrapplePoint = Instantiate(GrapplePoint, hit.point, Quaternion.identity);
copyGrapplePoint.name = "newGrapplePoint" + direction;
}
grappling = true;
grapplePoint = hit.point;
Rigidbody rb = player.GetComponent<Rigidbody>();
Vector3 directionToGrapplePoint = GameObject.Find("newGrapplePoint" + direction).transform.position - transform.position;
rb.AddForce(directionToGrapplePoint.normalized * grappleForce, ForceMode.Impulse);
}
}
else if (attackKeybind.action.WasReleasedThisFrame())
{
StopGrapple();
}
SetAnimations();
}
//Called after Update
void LateUpdate()
{
DrawRope();
}
public void ChangeAnimationState(string newState)
{
if (currentAnimationState == newState) return;
currentAnimationState = newState;
animator.CrossFadeInFixedTime(currentAnimationState, 0.2f);
}
void SetAnimations()
{
if (!grappling)
{
if (direction == "Left")
{
ChangeAnimationState(IDLE);
}
else
{
ChangeAnimationState(IDLER);
}
}
else
if (direction == "Left")
{
ChangeAnimationState(GRAPPLE);
}
else
{
ChangeAnimationState(GRAPPLER);
}
}
void StopGrapple()
{
Destroy(GameObject.Find("newGrapplePoint" + direction));
grappling = false;
lr.positionCount = 0;
}
void DrawRope()
{
if (!grappling) return;
lr.positionCount = 2;
lr.SetPosition(0, grappleOrigin.position);
lr.SetPosition(1, GameObject.Find("newGrapplePoint" + direction).transform.position);
}
}
This is my first time using the site, so please correct me if I’m using this incorrectly.