- Home /
Smooth grappling hook movement
I've managed to create a grappling hook mostly from scratch, though not nearly the way I originally intended for it to be. The script brings the player towards a Vector3, but it uses the transform.position instead of the .Move() method available for characterControllers.
I think this is the main cause of the stuttery movement that I'm getting with the current script. It brings the player forwards without smoothing the movement, because it's just changing the transform, instead of gradual/smoothed movement.
I've also seen/heard that using two movement functions at once on the same object can cause an issue with this as well, but I don't believe that is the cause. With the following code as a reference, is there any possible way I can make a characterController smoothly move towards a vector3 coordinate without making the movement seem shakey, stuttery, or for lack of a better terms, too sudden of intervals?
public class harpoonLauncher : MonoBehaviour
{
private bool collided = false;
private CharacterController playerReference;
private Transform playerTransform;
private Rigidbody harpoonHead;
//No instantiation, harpoonHead needs to be a child object of the character, that is never created or destroyed.
//just transformed.
private void Start()
{
harpoonHead = GetComponent<Rigidbody>();
harpoonHead.AddRelativeForce(0f, 0f, 1000f);
playerTransform = (GameObject.FindWithTag("PlayerCharacter")).transform;
playerReference = (GameObject.FindWithTag("PlayerCharacter")).GetComponent<CharacterController>();
}
private void OnCollisionEnter(Collision collision)
{
harpoonHead.velocity = Vector3.zero;
harpoonHead.constraints = RigidbodyConstraints.FreezeAll;
collided = true;
}
private void Update()
{
if (collided)
{
playerTransform.position = Vector3.MoveTowards(playerTransform.position, harpoonHead.transform.position, 3f * Time.deltaTime * Vector3.Distance(playerTransform.position, harpoonHead.transform.position));
}
}
}