Search Unity

Block player movement

Discussion in 'Scripting' started by Timoty75, Mar 20, 2019.

Thread Status:
Not open for further replies.
  1. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    Hi guys I need help: I create a isometric player whit function Onmousedown for the movente but I wont that when (for example..the player interact with NPG...always with function Onmousedown), blocks the player movement and don't walk to the direction of the click/function? How can I do? Thanks a lot
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Just exit the method when you don't want it to move;

    Code (CSharp):
    1. public bool PreventMovement {get; set;}
    2.  
    3. public void OnMouseDown()
    4. {
    5.     if(PreventMovement)
    6.         return;
    7.  
    8.     //Reset of your code
    9. }
     
  3. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    [QUOTE = "WarmedxMints, post: 4339006, member: 1286778"] Esci dal metodo quando non vuoi che si muova;

    [code = CSharp] public bool PreventMovement {get; impostato;}

    pubblico vuoto OnMouseDown ()
    {
    if (PreventMovement)
    ritorno;

    // Ripristino del codice
    } [/ Code] [/ QUOTE]
    [QUOTE = "WarmedxMints, post: 4339006, member: 1286778"] Esci dal metodo quando non vuoi che si muova;

    [code = CSharp] public bool PreventMovement {get; impostato;}

    pubblico vuoto OnMouseDown ()
    {
    if (PreventMovement)
    ritorno;

    // Ripristino del codice
    } [/ Code] [/ QUOTE]
    ... And for return to movement in a second time?
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    You control Player movement by setting the value of PreventMovement. If it is set to TRUE you player will not move, set it to FALSE, and it will move on a mouse click.

    Obviously, you now need code to correctly set and re-set PreventMovement, but we know precious little about your code and game other than that you want to inhibit movement sometimes, and WarmedxMints gave their best shot :)
     
  5. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    [QUOTE = "csofranz, post: 4339129, member: 1365796"] Controlli il movimento del giocatore impostando il valore di PreventMovement. Se è impostato su TRUE, il giocatore non si muoverà, lo imposterà su FALSE e si muoverà con un clic del mouse.

    Ovviamente, ora hai bisogno del codice per impostare e reimpostare correttamente PreventMovement, ma conosciamo molto poco del tuo codice e del tuo gioco, a parte il fatto che vuoi inibire i movimenti a volte, e WarmedxMints ha dato il loro colpo migliore :)[/ QUOTE]
    Grazie mille csofranz..I try
     
  6. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    [QUOTE = "WarmedxMints, post: 4339006, member: 1286778"] Esci dal metodo quando non vuoi che si muova;

    [code = CSharp] public bool PreventMovement {get; impostato;}

    pubblico vuoto OnMouseDown ()
    {
    if (PreventMovement)
    ritorno;

    // Ripristino del codice
    } [/ Code] [/ QUOTE]
    Grazie mille WarmedxMints!
     
  7. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    It does't work...what is wrong?


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class elfo : MonoBehaviour
    7. {
    8.     public bool vClickToMoveInput;
    9.     public GameObject dialogue;
    10.     public GameObject cursor;
    11.     public bool isFound;
    12.     public static GameObject instance;
    13.  
    14.  
    15.  
    16.     private void Awake()
    17.     {
    18.         instance = this;
    19.     }
    20.  
    21.  
    22.     void Start()
    23.     {
    24.      
    25.      
    26.  
    27.         dialogue= FindObjectOfType<GameObject>();
    28.         cursor  = FindObjectOfType<GameObject>();
    29.     }
    30.  
    31.  
    32. public   void OnMouseDown()
    33.     {
    34.         dialogue.SetActive(true);
    35.         cursor.SetActive(false);
    36.         {
    37.             if (vClickToMoveInput)
    38.                 return;
    39.  
    40.             //Reset of your code
    41.         }
    42.         StartCoroutine(LateCall());
    43.     }
    44.  
    45.     IEnumerator LateCall()
    46.     {
    47.  
    48.         yield return new WaitForSeconds(3);
    49.         cursor.SetActive(true);
    50.         dialogue.SetActive(false);
    51.     }
    52.  
    53.     }
    54.  
     
  8. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Code (CSharp):
    1.  
    2. dialogue= FindObjectOfType<GameObject>();
    3. cursor  = FindObjectOfType<GameObject>();
    4.  
    for starters, "dialogue" and "cursor" are set to the same object, so they'll both be inactive after

    Code (CSharp):
    1.  
    2. dialogue.SetActive(true);
    3. cursor.SetActive(false);
    4.  
    But you should tell us what doesn't work: what you expect to happen, and what happens instead.
     
  9. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    That code shouldn't even compile. Its type is elfo not a gameobject so you should have an error on line 18 for starters. Although there is also a lot wrong after that.
     
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Are your first two ever posts really necroing a 3 year old thread just to reply that you dont know a solution either and to post some weird confused smiley stickman, in 2 separate posts? You got to be trolling.
     
Thread Status:
Not open for further replies.