Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved I need to make my character stop, when talking to npc

Discussion in 'Scripting' started by Silvaantonio, Sep 6, 2021.

  1. Silvaantonio

    Silvaantonio

    Joined:
    Apr 29, 2021
    Posts:
    9
    Hi, thanks in advance for stopping by. I have a simple player movement, and a generic NPC (unity RPG asset). I can't get my player to stop while talking to the NPC. Here's the code for the player movement, NPC controller, and input controller. I've experimented a lot, and can't get it working

    PlayerMovement:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.     public bool canMove;
    10.  
    11.     public float moveSpeed = 5f;
    12.  
    13.     public Rigidbody2D RigidB;
    14.  
    15.     public Animator animator;
    16.  
    17.     Vector2 movement;
    18.  
    19.  
    20.     void Update()
    21.     {
    22.         movement.x = Input.GetAxisRaw("Horizontal");
    23.         movement.y = Input.GetAxisRaw("Vertical");
    24.  
    25.         animator.SetFloat("Horizontal", movement.x);
    26.         animator.SetFloat("Vertical", movement.y);
    27.         animator.SetFloat("Speed", movement.sqrMagnitude);
    28.     }
    29.  
    30.     void FixedUpdate()
    31.     {
    32.         RigidB.MovePosition(RigidB.position + movement * moveSpeed * Time.fixedDeltaTime);
    33.     }
    34.  
    35. }
    Npc controller:
    Code (CSharp):
    1. using RPGM.Core;
    2. using RPGM.Gameplay;
    3. using UnityEngine;
    4.  
    5. namespace RPGM.Gameplay
    6. {
    7.     /// <summary>
    8.     /// Main class for implementing NPC game objects.
    9.     /// </summary>
    10.     public class NPCController : MonoBehaviour
    11.     {
    12.         public ConversationScript[] conversations;
    13.  
    14.         Quest activeQuest = null;
    15.  
    16.         Quest[] quests;
    17.  
    18.         GameModel model = Schedule.GetModel<GameModel>();
    19.  
    20.         void OnEnable()
    21.         {
    22.             quests = gameObject.GetComponentsInChildren<Quest>();
    23.         }
    24.  
    25.         public void OnCollisionEnter2D(Collision2D collision)
    26.         {
    27.             var c = GetConversation();
    28.             if (c != null)
    29.             {
    30.                 var ev = Schedule.Add<Events.ShowConversation>();
    31.                 ev.conversation = c;
    32.                 ev.npc = this;
    33.                 ev.gameObject = gameObject;
    34.                 ev.conversationItemKey = "";
    35.             }
    36.         }
    37.  
    38.         public void CompleteQuest(Quest q)
    39.         {
    40.             if (activeQuest != q) throw new System.Exception("Completed quest is not the active quest.");
    41.             foreach (var i in activeQuest.requiredItems)
    42.             {
    43.                 model.RemoveInventoryItem(i.item, i.count);
    44.             }
    45.             activeQuest.RewardItemsToPlayer();
    46.             activeQuest.OnFinishQuest();
    47.             activeQuest = null;
    48.         }
    49.  
    50.         public void StartQuest(Quest q)
    51.         {
    52.             if (activeQuest != null) throw new System.Exception("Only one quest should be active.");
    53.             activeQuest = q;
    54.         }
    55.  
    56.         ConversationScript GetConversation()
    57.         {
    58.             if (activeQuest == null)
    59.                 return conversations[0];
    60.             foreach (var q in quests)
    61.             {
    62.                 if (q == activeQuest)
    63.                 {
    64.                     if (q.IsQuestComplete())
    65.                     {
    66.                         CompleteQuest(q);
    67.                         return q.questCompletedConversation;
    68.                     }
    69.                     return q.questInProgressConversation;
    70.                 }
    71.             }
    72.             return null;
    73.         }
    74.     }
    75. }
    Input controller:
    Code (CSharp):
    1. using RPGM.Core;
    2. using RPGM.Gameplay;
    3. using UnityEngine;
    4.  
    5. namespace RPGM.UI
    6. {
    7.     /// <summary>
    8.     /// Sends user input to the correct control systems.
    9.     /// </summary>
    10.     ///
    11.  
    12.  
    13.     public class InputController : MonoBehaviour
    14.     {
    15.         Vector2 movement;
    16.         public float moveSpeed = 2f;
    17.         public float stepSize = 0.1f;
    18.         public Animator animator;
    19.  
    20.         GameModel model = Schedule.GetModel<GameModel>();
    21.  
    22.         public enum State
    23.         {
    24.             PlayerMovement,
    25.             DialogControl,
    26.             Pause
    27.         }
    28.  
    29.         State state;
    30.  
    31.         public void ChangeState(State state) => this.state = state;
    32.  
    33.         void Update()
    34.         {
    35.             switch (state)
    36.             {
    37.                 case State.PlayerMovement:
    38.                     PlayerMovement();
    39.                     break;
    40.                 case State.DialogControl:
    41.                     DialogControl();
    42.                     break;
    43.             }
    44.         }
    45.  
    46.         void DialogControl()
    47.         {
    48.             if (Input.GetKeyDown(KeyCode.LeftArrow))
    49.                 model.dialog.FocusButton(-1);
    50.             else if (Input.GetKeyDown(KeyCode.RightArrow))
    51.                 model.dialog.FocusButton(+1);
    52.             if (Input.GetKeyDown(KeyCode.Space))
    53.                 model.dialog.SelectActiveButton();
    54.         }
    55.  
    56.         void PlayerMovement()
    57.         {
    58.  
    59.         }
    60.     }
    61.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Usually one would inhibit the motion during certain portions of the game.

    The simplest is to have a boolean somewhere that you check in the player Update() method and simply disable motion while the dialog is in progress.

    This can be as simple as a global boolean, or as complicated as some global manager that keeps track of what all should be allowed to operate and is aware of all possible states of your game, entirely up to you.
     
  3. Silvaantonio

    Silvaantonio

    Joined:
    Apr 29, 2021
    Posts:
    9
    Thank you, ill work form there and if i need something ill reply here. :) ty <3