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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.Characters.FirstPerson; public class Hool : MonoBehaviour { public Transform cam; private RaycastHit hit; private Rigidbody rb; public bool attached = false; public float momentum; public float speed; private float step; public RigidbodyFirstPersonController cc; LineRenderer line; public GameObject m_endorb; public float maxDistance; public GameObject shootfrom; bool groundedLastFrame; // Use this for initialization void Start() { rb = GetComponent <Rigidbody>(); line = GetComponent<LineRenderer>(); } // Update is called once per frame void Update() { if (Input.GetButtonDown("Fire2")) { if (Physics.Raycast(cam.position, cam.forward, out hit, maxDistance)) { if (hit.transform.gameObject.tag == "Gyes") { attached = true; cc.mouseLook.XSensitivity = 5; cc.mouseLook.YSensitivity = 5; GameObject cube2Instance; cube2Instance = Instantiate(m_endorb, hit.point, Quaternion.identity); rb.isKinematic = true; } } } if (Input.GetButtonUp("Fire2")) { if (hit.transform.gameObject.tag == "Gyes") { cc.mouseLook.XSensitivity = 5; cc.mouseLook.YSensitivity = 5; attached = false; rb.isKinematic = false; rb.velocity = transform.forward * momentum; } } line.enabled = attached; if (attached) { momentum += Time.deltaTime * speed * 1.25f; step = momentum * Time.deltaTime; transform.position = Vector3.MoveTowards(transform.position, hit.point, step); line.SetPosition(0, shootfrom.transform.position); line.SetPosition(1, hit.point); } if (!attached && momentum >= 0) { momentum -= Time.deltaTime * 5; step = 0; } // if we've just landed this frame, zero momentum if (!groundedLastFrame && cc.Grounded && momentum >= 0) { momentum = 0; step = 0; } groundedLastFrame = cc.Grounded; } }
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).