• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_9srkGCLMoq9gmQ · Oct 30, 2018 at 10:00 PM · unity 2drpg

How do I stop my player movement?,How do I stop the player movement for a while?

Hi I'm using unity for the first time and have no clue about c# etc. I would like to prevent my player from moving while a dialogue box is showing. I'm using the RPGTalk asset which allows me to call a function from a script while the text ist showing. I don't really know what to put after "else" and I would be very grateful if someone could help me.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
 
     public float speed;
     public bool controls;
 
     private Rigidbody2D rb;
     private Vector2 moveVelocity;
     private Animator anim;
 
     void Start()
     {
 
         rb = GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>();
 
 
 
     }
 
     void Update()
     {
         if (controls)
         {
 
             Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
             moveVelocity = moveInput.normalized * speed;
         }
     } else {
         
 
     public void CancelControls()
     {
         controls = false;
     }
 
     public void GiveBackControls()
     {
         controls = true;
     }
 
 
 
     void FixedUpdate()
         {
             rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
 
         }
     }
 

,I'm working with Unity for the first time and I'm a total noob in C#. I wanted to prevent the player from moving while the textbox is showing. I'm using the RPGTalk asset which allows me to call a function from a script when the talk begins and ends. Does anyone have an idea how to stop the player movement after "else"?

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float speed; public bool controls; private Rigidbody2D rb; private Vector2 moveVelocity; private Animator anim; void Start() { rb = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); } void Update() { if (controls) { Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); moveVelocity = moveInput.normalized * speed; } } else { public void CancelControls() { controls = false; } public void GiveBackControls() { controls = true; } void FixedUpdate() { rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime); } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Casiell · Oct 31, 2018 at 09:12 AM

Setting your moveVelocity to Vector2.Zero should probably be enough.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_9srkGCLMoq9gmQ · Nov 01, 2018 at 09:55 PM 0
Share

I tried to write moveVelocity = Vector2.Zero after }else{ but it doesn't seem to work

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

good tutorials for 2d action rpg?,Good tutorials on 2D action-rpg? 1 Answer

UI RPG Dialog Sytem 0 Answers

In Unity 2d is it possible to change which sprite sheet a player is using? 0 Answers

Unity2D Sprite lighting Diffuse Material is just black 3 Answers

Sprites rendering strangely at different resolution 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges