• 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 stowellgray · Jan 08 at 10:15 PM · movement

I fly, why?,I fly why?

I fly up when i look and press a movement key. But i dont want to go up. I just want a normal walking script Heres mine. public class PlayerMove : MonoBehaviour { public float moveSpeed = 2.0f; public Transform playerBody; public Transform mainCamera; public CharacterController controller;

     // Update is called once per frame
     void Update()
     {
         KeyboardMovement();
     }
     void KeyboardMovement()
     {
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
         Vector3 sidewaysPos = Camera.main.transform.right;
         Vector3 forwardPos = Camera.main.transform.forward;
         Vector3 move = sidewaysPos * x + forwardPos * z;
         transform.Translate(move * moveSpeed * Time.deltaTime);
     }
 }

,I want a normal movement script. I've kinda made my own because the other ones wouldn't work. But whenever i look up iand press w (any movement key) I go in the air. I want to fix this but idk how.

Heres my code public class PlayerMove : MonoBehaviour { public float moveSpeed = 2.0f; public Transform playerBody; public Transform mainCamera; public CharacterController controller;

 // Update is called once per frame
 void Update()
 {
     KeyboardMovement();
 }
 void KeyboardMovement()
 {
     float x = Input.GetAxis("Horizontal");
     float z = Input.GetAxis("Vertical");
     Vector3 sidewaysPos = Camera.main.transform.right;
     Vector3 forwardPos = Camera.main.transform.forward;
     Vector3 move = sidewaysPos * x + forwardPos * z;
     transform.Translate(move * moveSpeed * Time.deltaTime);
 }

}

Comment
Add comment · Show 1
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 stowellgray · Jan 08 at 01:55 PM 0
Share

The variables are: public float moveSpeed = 2.0f; public Transform playerBody; public Transform mainCamera; public CharacterController controller;

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Jan 08 at 10:29 PM

You really don't want to use transform.Translate. If you're already using a character controller, use the Move() function on the controller.


You would have to get the correct directions when a key is pressed, however. Right now, you're getting the camera's forward direction, which is the look direction. So you'll just move where you're looking, no matter the axis. You only really want to do that for the y-axis. The x and z axis should be controlled using Input.GetAxis("Horizontal"); or something like that.


There are already some great example character controller movement scripts and tutorials on youtube, Unity Learn, the Docs, and elsewhere, so I would go check some of those out if you can't understand what I wrote. @stowellgray

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 unity_ek98vnTRplGj8Q · Jan 08 at 10:32 PM

You are using the forward, left, and right direction of the camera, so if you point the camera up, then forwardPos in your script will be up and that is where you will move. Usually the way to do this is to have a player object that can only look left or right but not up or down. Then as a child of this you have the camera. Moving the camera left and right will turn the whole character left and right, but moving the camera up and down will only move the camera up and down. Then just use the trasform.right and transform.forward of your character and NOT your camera.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

166 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

Related Questions

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

Making a bubble level (not a game but work tool) 1 Answer

Handling Animation and movement speed 0 Answers

Player Controller moves automatically forward. cant stop. 1 Answer

How do I make speed constant in between position nodes? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges