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 /
  • Help Room /
avatar image
0
Question by Xaverix · Jan 28, 2017 at 03:24 PM · c#playermoving

Best way of moving player

Hi. I'm making some sort of Minecraft clone (i know... i know...) and I have question about player movement. Every method I used has the same problem. If i fall down and stick into wall my player is glitching or even stopping.

What is the best way to control the player and avoid these problems? I could disable moving if player is not touching ground but I want some control in air and it is important because if i jump i want to move in air to the next block.

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
1

Answer by whereswaldo · Jan 28, 2017 at 05:42 PM

I'd recommend using unity's First Person Character Controller (which can be found in the 'characters' folder when you go to the import assets tab in the editor) with a sphere collider around the camera. It's simple but should fix all the issues you're having.

Comment
Add comment · 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
0

Answer by Loui_Studios · Jan 28, 2017 at 03:36 PM

Well, there is really no best way to control a character on screen. It all depends on what kind of game you're making.

Since you're making a Minecraft clone, I don't think realism or physics-based movement matters that much, so I'd recommend a simple First Person Character Controller.

Attach a character controller to your player, and then create a c# script called FPSController.

Just paste my code into it and the rest is pretty straightforward.

 using UnityEngine;

 public class FPSController : MonoBehaviour {
 
     public CharacterController characterController;
 
     [Header("Camera")]
     public Camera cam;
     public float XSensitivity, YSensitivity;
     public bool invertPitch, clamp;
     public int minY, maxY;
 
     [Header("Movement")]
     public bool grounded, jumping;
     public float forwardSpeed, horizontalSpeed, jumpPower, gravity;
     
     [HideInInspector] public float jumpSpeed, verticalRotation;
 
     void Update() {
         float mouseX = Input.GetAxis("Mouse X"), mouseY = Input.GetAxis("Mouse Y");
 
         if (!invertPitch)
             mouseY = -mouseY;
         verticalRotation += mouseY * YSensitivity;
         if (clamp)
             verticalRotation = Mathf.Clamp(verticalRotation, minY, maxY);
 
         transform.Rotate(0, mouseX * XSensitivity, 0);
         cam.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
 
         if (Input.GetMouseButtonDown(0))
             Cursor.lockState = CursorLockMode.Locked;
 
         float horizontal = Input.GetAxis("Horizontal"), forward = Input.GetAxis("Vertical");
 
         grounded = characterController.isGrounded;
 
         if (grounded) {
             jumping = false;
             jumpSpeed = 0;
         }
         else
             jumpSpeed -= (gravity * 25) * Time.deltaTime;
 
         if (Input.GetAxisRaw("Jump") != 0 && !jumping) {
             jumping = true;
             jumpSpeed = jumpPower;
         }
 
         Vector3 motion = new Vector3(horizontal * horizontalSpeed, jumpSpeed, forward * forwardSpeed);
         characterController.Move((transform.rotation * motion) * Time.deltaTime);
     }
 }

Let me know how this works for you, and if you have any problems!

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 Xaverix · Jan 28, 2017 at 04:00 PM 0
Share

Thank You but I don't want to use Character Controller because it has capsule collider, it's $$anonymous$$ecraft style game so i want to use box collider for player collider

avatar image Loui_Studios Xaverix · Jan 28, 2017 at 04:02 PM 0
Share

I don't think that would be very much fun to use. Besides, doesn't $$anonymous$$inecraft already use a capsule collider?

avatar image KHarladson91194 · Aug 06, 2020 at 06:17 AM 0
Share

I love this code. Thank you. I got this to work with the following Heirarchy

Player ( 3D Object(s) )

|-------- PlayerPOV (Camera)

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

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

How do I make my player's movement relative to Main Camera's direction? 1 Answer

Save/Load Player System 0 Answers

How can i make my player look at the same direction where its moving? [JOYSTICK ANDROID] 0 Answers

Player not moving in the right direction instantly 1 Answer

How do i ajust the player height acording to the height of the roof in a first person game? 1 Answer


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