namespace Opsive.UltimateCharacterController.Character.Abilities
{
using UnityEngine;
public class GrapplingGun : Ability
{
[SerializeField]
protected Transform camera;
[SerializeField]
private float maxDistance = 100f;
[SerializeField]
private LineRenderer lr;
private Vector3 grapplePoint;
public LayerMask whatIsGrappleable;
public Transform gunTip;
private SpringJoint joint;
private Vector3 currentGrapplePosition;
protected PlayerManager pm;
public override void Start()
{
base.Start();
pm = m_GameObject.GetComponent<PlayerManager>();
}
protected override void AbilityStarted()
{
base.AbilityStarted();
StartGrapple();
}
protected override void AbilityStopped(bool force)
{
base.AbilityStopped(force);
StopGrapple();
}
public override void LateUpdate()
{
base.LateUpdate();
DrawRope();
}
void StartGrapple()
{
RaycastHit hit;
if (Physics.Raycast(camera.position, camera.forward, out hit, maxDistance, whatIsGrappleable))
{
grapplePoint = hit.point;
joint = m_GameObject.gameObject.AddComponent<SpringJoint>();
joint.autoConfigureConnectedAnchor = false;
joint.connectedAnchor = grapplePoint;
float distanceFromPoint = Vector3.Distance(m_GameObject.transform.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;
pm.DestroyComponent(joint);
}
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);
}
}
}
hmm, but i use a character controller and it works only with ucc it doesn't work like ucc blocks gravity or anything like thatAs spring joints are a part of Unity and not UCC specifically this isn't the place to ask for help I'm afraid. I'm sure someone over at the Unity forums will be able to help.
Can you tell me what is PlayerManager Component?yeah i already made it and when my spawn spring joint doesn't work, is there anything i miss? This is an example script
Code:namespace Opsive.UltimateCharacterController.Character.Abilities { using UnityEngine; public class GrapplingGun : Ability { [SerializeField] protected Transform camera; [SerializeField] private float maxDistance = 100f; [SerializeField] private LineRenderer lr; private Vector3 grapplePoint; public LayerMask whatIsGrappleable; public Transform gunTip; private SpringJoint joint; private Vector3 currentGrapplePosition; protected PlayerManager pm; public override void Start() { base.Start(); pm = m_GameObject.GetComponent<PlayerManager>(); } protected override void AbilityStarted() { base.AbilityStarted(); StartGrapple(); } protected override void AbilityStopped(bool force) { base.AbilityStopped(force); StopGrapple(); } public override void LateUpdate() { base.LateUpdate(); DrawRope(); } void StartGrapple() { RaycastHit hit; if (Physics.Raycast(camera.position, camera.forward, out hit, maxDistance, whatIsGrappleable)) { grapplePoint = hit.point; joint = m_GameObject.gameObject.AddComponent<SpringJoint>(); joint.autoConfigureConnectedAnchor = false; joint.connectedAnchor = grapplePoint; float distanceFromPoint = Vector3.Distance(m_GameObject.transform.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; pm.DestroyComponent(joint); } 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); } } }