how to make a grappling hook?

Reza

Member
I want to make a grappling hook, which I know to make this with a spring join, but when I try it on a UCC character with the ability the spring join doesn't work.

is there a correct way for this to work?
 
I have seen that I understand that, the problem of the grapple system, that is how to apply to the ability there they only discuss the initial mechanics and that I can make but not discuss the core, such as the example of using a spring join to hang, or there are other ways
 
Spring joints do sound like a good place to start. I would imagine you'd want to start by creating your own custom item ability that spawns/activates a spring joint at a target location and connects it to the character's rigidbody.
 
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);
        }
    }
}
 
As 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.
 
As 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.
hmm, but i use a character controller and it works only with ucc it doesn't work like ucc blocks gravity or anything like that
 
CharacterLocomotion defines how all character movement is handled. It handles all the movement and rotation manually, so it uses a kinematic rigidbody. This means that spring joints are actually probably not applicable (unless there's some part of spring joints that involves kinematic rigidbodies, but I'm not familiar with spring joints in general).

I would recommend taking a look through CharacterLocomotion and seeing how it handles forces etc. For example, CharacterLocomotion.AddExternalForce might be useful to you.
 
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);
        }
    }
}
Can you tell me what is PlayerManager Component?
 
Top