- Home /
How to make my player fall when the cursor is unlocked
Could some one help me out? I am trying to make my character fall down when my cursor is unlocked, but it goes all crazy and moves in random directions.
Player Controller:
public class PlayerCharactercontroller : NetworkBehaviour { [Header("Player Controll")] public float MoveSpeed; public float TurnSpeed; public float JumpForce; public float Gravity;
private CharacterController Controll;
private LockCursor Lock;
private float VerticalVelocity;
[SerializeField]
private Camera Cam;
void Start()
{
Controll = GetComponent<CharacterController>();
Lock = GetComponent<LockCursor>();
}
private void Update()
{
if (!isLocalPlayer)
{
return;
}
if (Controll.isGrounded)
{
VerticalVelocity = -Gravity * Time.deltaTime;
if (Input.GetKeyDown("space"))
{
VerticalVelocity = JumpForce;
}
}
else
{
VerticalVelocity -= Gravity * Time.deltaTime;
}
Vector3 moveDir = new Vector3(0, VerticalVelocity, 0);
moveDir.x = Input.GetAxis("Horizontal") * MoveSpeed;
moveDir.y = VerticalVelocity;
moveDir.z = Input.GetAxis("Vertical") * MoveSpeed;
Controll.Move(transform.TransformDirection(moveDir) * Time.deltaTime);
float _RotY = Input.GetAxisRaw("Mouse X");
Vector3 _RotateY = new Vector3(0f, _RotY, 0f) * TurnSpeed;
float _RotX = Input.GetAxisRaw("Mouse Y");
Vector3 _RotateX = new Vector3(_RotX, 0f, 0f) * TurnSpeed;
transform.Rotate(_RotateY);
Cam.transform.Rotate(-_RotateX);
}
}
Lock:
public class LockCursor : MonoBehaviour { public bool Lock;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.lockState = CursorLockMode.Confined;
Cursor.visible = false;
Lock = true;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (!Lock)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.lockState = CursorLockMode.Confined;
Cursor.visible = false;
Lock = true;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
Lock = false;
}
}
}
}
Answer by K_Tec · Feb 23, 2017 at 07:39 PM
What do you mean with fall down???
Do you mean your character is flying and if the cursor is unlocked he gets gravity??
Like my player jumped up and is falling back down to the ground. If I jump then press the escape button to unlock, the player goes crazy.
That Lock Cursor class is all wrong... Where did you get this code from? You declare a bool Lock but never use it correctly...
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape)) { if (!Lock) // You never change the lock outside this statement, so the code inside will never ever never will be read because it's always true, which is what you set in first place...
// If Escape set Lock to the oposite it is now.
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape)) {Lock = !Lock}
if (!Lock)
{
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.lockState = CursorLock$$anonymous$$ode.Confined;
Cursor.visible = false;
}
else
{
Cursor.lockState = CursorLock$$anonymous$$ode.None;
Cursor.visible = true;
}
Well, I made this. I am a beginner at coding. Thanks for the help on the lock. How would I use the lock in my PlayerCharactercontroll script so when I press escape, say in the air, and the player falls down, but the player and the camera can't move?
You mean that when you press Escape you freeze or pause the game? All you have to do is search pause game either here or youtube... you'll find plenty of examples that you can study and start applying...
http://answers.unity3d.com/questions/249886/time-freeze-pause-game.html
and here is a quick post...
Answer by Mythran · Feb 24, 2017 at 08:30 AM
if (Controll.isGrounded)
{
VerticalVelocity = -Gravity * Time.deltaTime;
if (Input.GetKeyDown("space"))
{
VerticalVelocity = JumpForce;
}
}
else
{
VerticalVelocity -= Gravity * Time.deltaTime;
}
Have you noticed you're applying gravity wherever you're grounded or not?
if (Input.GetAxisRaw("Jump") != 0 && isGrounded) {
VerticalVelocity = jumpForce;
}
VerticalVelocity-= gravity * Time.deltaTime;
moveDir.y = VerticalVelocity;
That code is very confusing...
//Vector3 moveDir = new Vector3(0, VerticalVelocity, 0);
//moveDir.x = Input.GetAxis("Horizontal") * MoveSpeed;
//moveDir.y = VerticalVelocity;
//moveDir.z = Input.GetAxis("Vertical") * MoveSpeed;
//Controll.Move(transform.TransformDirection(moveDir) * Time.deltaTime);
Vector3 moveDir = (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))
moveDir *= MoveSpeed;
Controll.Move(transform.TransformDirection(moveDir) * Time.deltaTime);
I have noticed it, but it worked for just fine. Thanks for the shorten version of it. As for the movement part, your second block of code after you said it was confusing, I tried to put that in and I get errors.
that's because you must place:
Vector3 moveDir = (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))
before wherever you're calling it... since you start calling when you're checking gravity you should place it before it... or in the first line of Update() or just set Vector3 a field and then place moveDir = (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) inside LateUpdate()
Your answer
Follow this Question
Related Questions
Cursor unlocking on player 1 Answer
How do I jump? 0 Answers
Transform player from one position to another after completing a goal 1 Answer
Zig Zag movement Unity 2 Answers
Any way at all to center the cursor? 2 Answers