- Home /
Stop player from moving with WASD while using mouse still.
I want to freeze the player movement with the WASD keys and be able to still use the mouse to look around. I'm essentially opening an item in-game and want this action to freeze the players movement so that the player can look at the item and move the view around.
This is the code I'm using to start the map with the item hidden and pushing "m" will bring up the item.
using UnityEngine;
using System.Collections;
public class mapToggle : MonoBehaviour {
// Use this for initialization
void Start () {
renderer.enabled=false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.M))
{
// toggle visibility:
renderer.enabled = !renderer.enabled;
}
}
}
If you are using the standard assets Character$$anonymous$$otor there is a canControl variable, and when that is set to false WASD movement is disabled, but the seperate mouse look script is unaffected.
put a variable at the top public Character$$anonymous$$otor char$$anonymous$$ot;
assign it in the editter, and in the update section under "toggle visibility" put
char$$anonymous$$ot.canControl=false;
then, add an "else" section to the if statement containing char$$anonymous$$ot.canControl=true;
Answer by Klown · Aug 07, 2013 at 06:53 PM
This is the working code, with the help and use of some of Chronicfail's code it's working now.
using UnityEngine;
using System.Collections;
public class mapToggle : MonoBehaviour {
public CharacterMotor charMot;
// Use this for initialization
void Start () {
renderer.enabled=false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.M))
{
// toggle visibility:
renderer.enabled = !renderer.enabled;
charMot.canControl = !renderer.enabled;
}
}
}
Answer by WizzDE · Aug 02, 2013 at 09:07 PM
I don't know how you're moving script works but here is a really simple way to do this: You create a public variable in your moving script which needs to be a boolean. Call it something like "canMove". Then you set an if around your movinf part. if (canMove){ //code for moving around } Then all you need to do is change the canMove variable to false whenever you dont want to be able to move: If (Input.GetKeyDown (KeyCode.M)){ GameObject.Find ("YourPlayer").GetComponent (nameOfYourMovingScript).canMove = false; } Hope I helped
Wizz
Your answer
Follow this Question
Related Questions
How to make ball sticky 1 Answer
,Storing Vector3 arrays in another array? 1 Answer
Freeze Character control on axis 0 Answers
How to prevent movement in a specific direction only? -1 Answers
Mouse Look Freeze on Pause? 1 Answer