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 goldwyn11 · Jun 21, 2019 at 09:34 AM · controllercodepage

How to make a 3D character move?

Howdy guys c: Very newbie question here, trying to teach myself unity. Made a stock character and gave him an idle and a walk animation for now, but I can't seem to figure out how to make him move with the WASD keys. My endgame goal is to be able to walk, jump, and maybe dodge roll if I can clear that hurdle, but for now my character is forever doing the idle shuffle. Is there a walkthrough that covers this that I missed, or does anyone have some advice? Been banging my head against my keyboard for awhile :s

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by metalted · Jun 21, 2019 at 03:06 PM

My advice is using a charactercontroller, because it is the least trouble for a beginner. The sample script on the unity docs already has a jump attached. So if you want to use it add a charactercontroller component to your object and and this script: https://docs.unity3d.com/ScriptReference/CharacterController.Move.html


For a simpler movement, maybe useful in other games, you could set the position of the transform directly:

 float horizontal = Input.GetAxis("Horizontal");
 float vertical = Input.GetAxis("Vertical");
 float speed = 5.0f;
 
 void Update(){
     transform.position = new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime;
 }

So this way of moving is very very basic, but it will give you an idea of how you can retrieve axis input to control your character.

Comment
Add comment · Show 4 · 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 goldwyn11 · Jun 21, 2019 at 08:09 PM 0
Share

Thank you for the quick response c: I tried applying a character controller and pasted in the script that comes with it, but my player now floats about a person and a half up into the air and the controller doesn't seem to have an effect. I'm doing this stuff in an empty scene so I don't goof my level up. Sorry if I'm missing something obvious, the character controller script is giving me an error message too

Character and error message (all of his pieces have a skinned mesh renderer attached: https://cdn.discordapp.com/attachments/343449258168549386/591721159989526658/unknown.png

Plane settings: https://cdn.discordapp.com/attachments/343449258168549386/591711183892119591/unknown.png

Scene from gameplay: https://cdn.discordapp.com/attachments/343449258168549386/591711267685793872/unknown.png

avatar image metalted goldwyn11 · Jun 25, 2019 at 06:13 AM 1
Share

Okay I see the problem here. The thing is, you need to make sure your name of your class matches the name of your file. The class is most likely not called CharacterController.$$anonymous$$ove, because I don't think dots are allowed in a class name. If you want to copy the complete code from the unity docs, you will have to call it ExampleClass, because in the script you see the following:

 public class ExampleClass : $$anonymous$$onoBehaviour


The reason your character is floating in the air is because of the charactercontroller itself. The collider of the charactercontroller is nothing more than a capsule collider. So you have to make sure that the size of the collider matches the size of your character. You can change those setting using the size, center and radius parameters on the charactercontroller component in the inspector.

avatar image goldwyn11 metalted · Jun 25, 2019 at 09:31 PM 1
Share

Thank you so much, that sorted those two issues out :D ... now I know that the walk animation isn't triggering xD Ah well, one headdesk at a time, I'll see if I can get this one sorted and if I can't then I'll open another thread. Thank you very much for your time, I'm slowly headbutting my way through this stuff. In the middle of making the start screen now c:

avatar image anilcankarabulut · Nov 22, 2020 at 10:51 AM 0
Share

If you change the last line into transform.position += new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime; it works for more diverse situations, because you are hardsetting 'y' to 0 when you press any of the WASD buttons but with += you would only add the new Vector to your gameobject's position, thus never changing the 'y' axis. This would e. g. allow you to jump and still move while airborne.

avatar image
1

Answer by BigBoss8281 · Nov 08, 2020 at 08:31 AM

CODE TO MAKE PLAYER MOVE MAKE NEW SCRIPT AND NAME IT playerMovement.cs AND COPY PASTE THIS SCRIPT:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerMovement : MonoBehaviour {
     public float speed = 25.0F;
     public float jumpSpeed = 8.0F; 
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     private float turner;
     private float looker;
     public float sensitivity = 5;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         CharacterController controller = GetComponent<CharacterController>();
         // is the controller on the ground?
         if (controller.isGrounded) {
             //Feed moveDirection with input.
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             //Multiply it by speed.
             moveDirection *= speed;
             //Jumping
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         turner = Input.GetAxis ("Mouse X")* sensitivity;
         looker = -Input.GetAxis ("Mouse Y")* sensitivity;
         if(turner != 0){
             //Code for action on mouse moving right
             transform.eulerAngles += new Vector3 (0,turner,0);
         }
         if(looker != 0){
             //Code for action on mouse moving right
             transform.eulerAngles += new Vector3 (looker,0,0);
         }
         //Applying gravity to the controller
         moveDirection.y -= gravity * Time.deltaTime;
         //Making the character move
         controller.Move(moveDirection * Time.deltaTime);
     }
 }

And then drag it to Player,You can also setting the speed,sensitivity,Jump and gravity

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 BigBoss8281 · Nov 08, 2020 at 08:33 AM 0
Share

And don't forget to give character controller in your player

avatar image abualsaad2 · Apr 04, 2021 at 08:50 PM 0
Share

thank you sooooooooooooo much. i allways forgot to give character controller and i had many mistakes in the script

avatar image unity_AC424B35CC0C50FD1A80 · Jan 21 at 07:30 PM 0
Share

If i use this script my whole character moves in the camera direction and if move the character bounces and my camera freaksout

avatar image
0

Answer by Llama_w_2Ls · Nov 22, 2020 at 04:05 PM

Dont forget Rigidbody controllers as well. By locking rotation of the rigidbody on all axis in the editor (so it doesnt fall over), you can use AddForce() on the transform.forward/right/left/back, depending on a button press. Also AddForce upwards for jumping.

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 Tarodev · May 27, 2021 at 07:28 AM

Super quick and informative explainer here: https://youtu.be/ELz_EG-s0jU

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

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

117 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

Related Questions

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Pushing rigidbody with FPS Controller 3 Answers

3rd person rigidbody controller help :( 1 Answer

Pause Menu upon Idle (timer & button issues C#) 0 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