Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 NovaDrox · Feb 23, 2017 at 06:38 PM · playerplayer movementcursorlock

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;
         }
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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??

Comment
Add comment · Show 5 · 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 NovaDrox · Feb 23, 2017 at 10:23 PM 0
Share

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.

avatar image Mythran · Feb 24, 2017 at 09:36 AM 0
Share

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;
          }

avatar image NovaDrox Mythran · Feb 24, 2017 at 01:24 PM 0
Share

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?

avatar image Mythran NovaDrox · Feb 24, 2017 at 04:00 PM 0
Share

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...

Show more comments
avatar image
0

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);

Comment
Add comment · Show 3 · 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 NovaDrox · Feb 24, 2017 at 01:50 PM 0
Share

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.

avatar image Mythran NovaDrox · Feb 24, 2017 at 04:11 PM 0
Share

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()

avatar image NovaDrox Mythran · Feb 24, 2017 at 04:15 PM 0
Share

Okay then.

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.

Follow this Question

Answers Answers and Comments

72 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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges