Search Unity

Trouble with Grappling hook

Discussion in 'Scripting' started by Jrusso17, May 30, 2017.

  1. Jrusso17

    Jrusso17

    Joined:
    Jul 1, 2016
    Posts:
    5
    So I plan to create a grapple hook and have. although i do have momentum it cant be applied upwards but instead i stop if heading up and fall while continuing in the horizontal direction. anyone able to point out some tips or help
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class Hool : MonoBehaviour
    8. {
    9.     public Transform cam;
    10.     private RaycastHit hit;
    11.     private Rigidbody rb;
    12.     public bool attached = false;
    13.     public float momentum;
    14.     public float speed;
    15.     private float step;
    16.     public RigidbodyFirstPersonController cc;
    17.     LineRenderer line;
    18.     public GameObject m_endorb;
    19.     public float maxDistance;
    20.     public GameObject shootfrom;
    21.  
    22.     bool groundedLastFrame;
    23.  
    24.     // Use this for initialization
    25.     void Start()
    26.     {
    27.         rb = GetComponent <Rigidbody>();
    28.         line = GetComponent<LineRenderer>();
    29.  
    30.        
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (Input.GetButtonDown("Fire2"))
    37.         {
    38.            
    39.             if (Physics.Raycast(cam.position, cam.forward, out hit, maxDistance))
    40.             {
    41.                 if (hit.transform.gameObject.tag == "Gyes")
    42.                 {
    43.                         attached = true;
    44.                         cc.mouseLook.XSensitivity = 5;
    45.                         cc.mouseLook.YSensitivity = 5;
    46.  
    47.                         GameObject cube2Instance;
    48.                         cube2Instance = Instantiate(m_endorb, hit.point, Quaternion.identity);
    49.                         rb.isKinematic = true;
    50.                 }
    51.             }
    52.         }
    53.         if (Input.GetButtonUp("Fire2"))
    54.         {
    55.             if (hit.transform.gameObject.tag == "Gyes")
    56.             {
    57.                     cc.mouseLook.XSensitivity = 5;
    58.                     cc.mouseLook.YSensitivity = 5;
    59.                     attached = false;
    60.                     rb.isKinematic = false;
    61.                     rb.velocity = transform.forward * momentum;
    62.             }
    63.         }
    64.  
    65.         line.enabled = attached;
    66.         if (attached)
    67.         {
    68.             momentum += Time.deltaTime * speed * 1.25f;
    69.             step = momentum * Time.deltaTime;
    70.             transform.position = Vector3.MoveTowards(transform.position, hit.point, step);
    71.             line.SetPosition(0, shootfrom.transform.position);
    72.             line.SetPosition(1, hit.point);
    73.         }
    74.         if (!attached && momentum >= 0)
    75.         {
    76.             momentum -= Time.deltaTime * 5;
    77.             step = 0;
    78.         }
    79.  
    80.         // if we've just landed this frame, zero momentum
    81.         if (!groundedLastFrame && cc.Grounded && momentum >= 0)
    82.         {
    83.             momentum = 0;
    84.             step = 0;
    85.         }
    86.  
    87.         groundedLastFrame = cc.Grounded;
    88.     }
    89. }
    90.  
     
  2. CrystalConflux

    CrystalConflux

    Joined:
    May 25, 2017
    Posts:
    107
    What kind of behaviour are you looking for? A direct-pull in a straight line towards the hit point, or more of a swinging motion? Looking at your code it appears you're going for the straight-line pull.

    I can't immediately see why you're dropping off the hook, but first of all I'd recommend moving your physics code into FixedUpdate rather than Update, as that's where rigid-body manipulating code is meant to be (In order to stay in sync with the physics engine).