🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

creating a movement disable method (and calling it in other scripts) - help

Started by
7 comments, last by Juliean 3 years, 11 months ago

hello everyone!

i'm trying to create a ‘disable’ method in the player movement script so that when certain things happen in other scripts, the method is called and the player stops moving. for instance, player dies = movement disabled. i've created a short method inside the ThirdPersonMovement script called Disable that contains a bool “disabled”, which inside the method is set to true.

afterwards i took all of the movement script and put it inside an if(!disable) statement. so far so good.

now i'm trying to call this method from a different script, by using ThirdPersonMovement.Disable(true); but i get an error saying "no overload of method ‘Disable’ takes 1 argument.

thanks, here is the movement script:

using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;


public class ThirdPersonMovement : MonoBehaviour

{


    public CharacterController controller;
    public Transform cam;
    public float speed = 6f;
    public float turnSmoothTime = 0.01f;
    float turnSmoothVelocity;
    bool disabled;

    void Update()
    {
		if (!disabled)
		{
            float horizontal = Input.GetAxisRaw("Horizontal");
            float vertical = Input.GetAxisRaw("Vertical");
            Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
            
            if (direction.magnitude >= 0.1f)
            {
                float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
                float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
                transform.rotation = Quaternion.Euler(0f, angle, 0f);

                Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
                controller.Move(moveDir.normalized * speed * Time.deltaTime);
            }
        }
    }

    public void Disable()
	{
        disabled = true;
	}

}

None

Advertisement

Uh, so this time I going to preface my solution with what I was going to say in your last thread: You should take a few steps back and look at some coding tutorials to get a basic understanding of the C# language (or programming languages as a whole), as those are some very basic mistakes that you can easily learn to avoid if you just take the time to, instead of jumping from problem to problem learn a few key concepts ?

The problem you are being faced with is that the Disable-method does not take a boolean. It takes no argument (as its argument list within () is empty). So if you want to be able to toggle a boolean class variable with a method, it must look like that:

public void Disable(bool disable) { this.disabled = disable;}

But yeah.. do yourself a favor and take a few hours/days of structured learning to boost your ability to handle those kind of things yourself ?

@Juliean guess you're right hmm… just finished about a week of learning from a tutorial where the guy Sebastian Lague) actually used the method i wrote so i figured it would work in other projects. guess i'm missing something.

None

There's a lot of nuance to situations like this. For example, if this case would not have been so clear, it would have been necessary to know the place were you called the method as well (so you should also post that part too).

Also, if you want to show me the specific part in the tutorial I could maybe tell you whats the difference (if you want).

@Juliean sure. i'll do both.

this is actully the same code from my other post, but i'll share it again any way. afterwards will be the link to the video

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WomanNPC : MonoBehaviour
{
    private GameObject triggeringPlayer;
    private bool triggering = false;
    public GameObject dialogueBox;
    //public string name = womanNPC;



    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (triggering)
        {
            if (Input.GetKey("e"))
            {
                //this works
                dialogueBox.SetActive(true);
                ThirdPersonMovement.Disable(true);
                
            }
        }
        if (Input.GetKey(KeyCode.Escape))
        {
            //this does not
            dialogueBox.SetActive(false);
            ThirdPersonMovement.Disable(true);
        }
    }


    //this sentence means that the program will check if the player is triggering with an NPC
    public void OnTriggerEnter(Collider other)
    {

        //this statement comes to check if the 'other' (which is the object we are colliding with) is tagged as "NPC", triggering is going to happen
        if (other.tag == "Player")
        {
            triggering = true;
            //this next line means the the other thing we are interacting with and it's definition is the triggering NPC private variable. 
            triggeringPlayer = other.gameObject;

            FindObjectOfType<TextSpawner>().OnTriggering();



        }


    }

    //this sentence means that the program will check if the player isn't triggering with an NPC
    public void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player")
        {
            triggering = false;
            triggeringPlayer = null;
            FindObjectOfType<TextSpawner>().OnNotTriggering();

        }
    }

    
}






i will for the record say i've started another tutorial playlist but this is my “progress project” that i come back to every few days to see if i can implement new things i've learned. it's getting there lol.

the part where he explains the disabled method starts at around 03:19.

thanks for the help dude. i very much appreciate it.

None

hm, I'm not seeing where he is using the method with a bool input around that time? He's only binding to an even, which calls it as-is without any parameters. I've skimmed through it so seems the tutorial is at least consistent ?

@Juliean well when i built his game it worked, maybe the rest of the video shows some other code that completes it? while i was looking about in my scripts i cou;dn't find other relevant things but i could be wrong.

i would like to ask you however for recommendations on tutorials, this is one of the best channels ive found so far but i'd love one that is more comprehensive in terms of teaching code.

learning by doing simple games is great, but if you know of more basic ones that really put an emphasis on c#, i'd be really happy to know. including from this site btw, i still haven't checked it thoroughly.

and once again thanks for your replies.

None

SassyPantsy said:
i would like to ask you however for recommendations on tutorials, this is one of the best channels ive found so far but i'd love one that is more comprehensive in terms of teaching code.

learning by doing simple games is great, but if you know of more basic ones that really put an emphasis on c#, i'd be really happy to know. including from this site btw, i still haven't checked it thoroughly.

Oh I'm sorry, I cannot really recommend you any tutorials on that matter. When I started C# I already had 10+ years of C++-experience, so my learning-process was by reading MSDN (the microsoft-documentation); but I don't think thats appropriate for beginners. Maybe somebody else can recommend something useful instead.

This topic is closed to new replies.

Advertisement