Home > Software design >  Cant get my player to jump /get detached from a rope
Cant get my player to jump /get detached from a rope

Time:03-16

I Have a player which gets childed to a game object when it walks up to a trigger now I want the player's parent to become null again after space is pressed because I'm trying to make a rope system, and it's required for the player to be able to de-attach from the rope

This is the script that's supposed to attach/detach the player from the rope

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttachToRope : MonoBehaviour
{
    public GameObject objectToParentTo;

    public GameObject objectWithSwingScript;



    // Start is called before the first frame update

    private void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.tag == "Rope")
        {
            transform.parent = objectToParentTo.transform;
            objectWithSwingScript.GetComponent<playerscript>().enabled = true;
            GetComponent<PlayerController>().enabled = false;
            GetComponent<CharacterController>().enabled = false;
            GetComponent<Swinging>().enabled = false;
        }
    }

    private void OnTriggerStay(Collider collider)
    {

        if (Input.GetButtonDown("Jump"))
        {
                transform.parent = null;
                objectWithSwingScript.GetComponent<playerscript>().enabled = false;
                GetComponent<PlayerController>().enabled = true;
                GetComponent<CharacterController>().enabled = true;
                GetComponent<Swinging>().enabled = true;
            Debug.Log("Deattached");
            }
    }
}

What happens when the player enters the trigger is that the scripts that make the player move get disabled and then it gets chilled to the last section of the rope now in ontriggerstay i want it to check if space is pressed and re-enable all the scripts that are required for the player to move (which does not work) but since nothing in there works i tried to debug.log but even that does not work so if anyone knows how to fix this please help me

CodePudding user response:

From the OnTriggerStay documentation: The function is on the physics timer so it won't necessarily run every frame.

Functions on the physics timer (e.g. FixedUpdate()) don't play nicely with Input.GetButtonDown because they don't run every frame. Instead, they run on a fixed timestep, 0.02 seconds by default.

The solution is to put calls to Input.GetButtonDown into Update(). For instance, in this example you could have a boolean member variable isJumpPushed and set it to true in Update() when the jump button is pushed and the player is attached, and then check that value in OnTriggerStay.

--

A note about debugging:

I tried to debug.log but even that does not work

If your Debug.Log isn't showing a log in the console, that still tells you something important. It tells you that code path isn't getting called. That's an important clue for you to figure out what's really going on. To further narrow down the problem, you could move the Debug.Log statement outside the if statement. This would show that it's Input.GetButtonDown that isn't returning true when you think it is.

  • Related