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 cmtrapp02 · Jul 16, 2017 at 08:19 PM · charactercontrollerprogramming

How to move character controller only on X and Z axis but not on Y.

Hello, I am very new to programming in Unity and programming in general, so please forgive me if I don't describe my question very well or if I ask extremely nooby questions.

I've set up an FPS player controller that moves using WASD, and can also look around using a mouse look script. I used a transformdirection so that, for example, if I look to the left and press W, I will move forward in that direction. The problem is that it does this on all three axis x y and z. I only want this to occur when looking left, right, or forwad. (X and Z I think, but correct me if I'm wrong.) What I don't want is to move upwards if I'm looking up or downwards if I'm looking down (Y axis)

So using the script I have, how can I make it so I move forwards where I'm looking that isn't up or down relative to the world?

Also since I'm very new to coding, if I have any problems with my script or if it's using some bad practices that I'm unaware of please let me know.

Here's my code:

 // Use this for initialization
 void Start () 
 {
     
     controller = GetComponent<CharacterController>();

 }
 
 // Update is called once per frame
 void Update ()
 {
     //WASD movement
     if(controller.isGrounded)
     {
         
         
         movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
         movement = transform.TransformDirection(movement);
         movement *= speed;

         
     }
    

     controller.Move(movement * Time.deltaTime);
     movement.y -= gravity * Time.deltaTime;

     //Mouse Look 
     if (axes == RotationAxes.MouseXAndY)
     {
         float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;

         rotationY += Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
         rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

         transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
     }
     else if (axes == RotationAxes.MouseX)
     {
         transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime, 0);
     }
     else
     {
         rotationY += Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
         rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

         transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
     }
     if (lockCursor)
     {
         Cursor.lockState = CursorLockMode.Locked;
     }


     //transform.Rotate(Input.GetAxis("Mouse Y") * new Vector3(sensitivityY, 0, 0) * Time.deltaTime);

     if (lockCursor)
     {
         Cursor.lockState = CursorLockMode.Locked;
         
     }
     
     

 }

   
 

} `

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cornelis-de-Jager · Jul 16, 2017 at 10:15 PM

Here is a simple script you can use:

 public float speed = 3.5f;
 
 void Move () {
 // Check if grounded
 if(!controller.isGrounded)
     return;

  // Get Axis Inputs
  float x = Input.GetAxis ("Horizontal") * Time.DeltaTime * speed;
  float z = Input.GetAxis ("Vertical") * Time.DeltaTime * speed;
 
  // Translate the object
  transform.Translate (x , 0, z);
 }

 public int turnDegrees = 2;
 
 void Rotate () {
  // Don't rotate in air 
  if(controller.isGrounded)
      return;
 
 // If Q and E are pressed
 if (Input.GetKey (KeyCode.Q)) 
    transform.Rotate (new Vector3 (0, -turnDegrees, 0));
 
 if (Input.GetKey (KeyCode.E)) 
    transform.Rotate (new Vector3 (0, turnDegrees, 0));
 }
 
 void LateUpdate () {
   Move (); // Handles Movement
  Rotate (); Handles Rotation - Might need to change this to your mouse look script
 }




Comment
Add comment · Show 1 · 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 cmtrapp02 · Jul 16, 2017 at 11:12 PM 0
Share

Thank you very much!

quick question if you don't $$anonymous$$d. How can I go about learning to code in unity without constantly copying other peoples scripts? so far anything I've done has been others work that may be slightly modified. Is just reading through the scripting API enough to get the knowledge I need, or something else?

either way, thanks for the help!

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

82 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

Related Questions

Multiple Cars not working 1 Answer

Spline with character enter 0 Answers

problem with ground check 1 Answer

Character goes through walls 3 Answers

Hi please tell me whats wrong with my player script. 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