• 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 Fewpwew130 · Dec 09, 2015 at 08:04 PM · fpscontrollerwaterflying

[script attached] Simple flying script

Hello, I am trying to remake an FPS Movement Controller script for a flying script. So that the gravity will have no impact at all and the player will be able to go up and down.

I created a camera, attached it in front of the player controller, but the script doesn't work as it should: if I press "A" - it goes to the right, if I press "D" - it also goes to the right. Same for the "W" and "S".

Could you please suggest what is wrong with the script?

 using UnityEngine;
 using System.Collections;
 
 public class watermovement : MonoBehaviour {
 
     // This component is only enabled for "my player" (i.e. the character belonging to the local client machine).
     // Remote players figures will be moved by a NetworkCharacter, which is also responsible for sending "my player's"
     //location to other computers.
     
     public float waterspeed = 10f;        // The speed at which I run
     public float waterjumpSpeed = 6f;    // How much power we put into our jump. Change this to jump higher.
     
     // Booking variables
     Vector3 direction = Vector3.zero;    // forward/back & left/right
     float   verticalVelocity = 0;        // up/down
     
     CharacterController cc;
     Animator anim;
 
     public Camera thecam;
 
 
     // Use this for initialization
     void Start () {
         cc = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         
         // WASD forward/back & left/right movement is stored in "direction"
 //        direction = transform.rotation * new Vector3( Input.GetAxis("Horizontal") ,0, Input.GetAxis("Vertical") );
 //        direction = thecam.transform.forward;
 
 //            if (Input.GetAxis("Vertical") != 0)
 //            {
 //        //        transform.Translate(Vector3.forward * waterspeed * Input.GetAxis("Vertical"));
 //                transform.Translate(thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
 //                
 //            }
 
 
         if (Input.GetKey(KeyCode.W)) {
             
             transform.Translate(thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
         }
     
         if (Input.GetKey(KeyCode.S)) {
             
             transform.Translate(-thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
         }
             
 
 
         if (Input.GetKey(KeyCode.A)) {
                 transform.Translate(Vector3.left * waterspeed * Input.GetAxis("Horizontal"));
         }
 
         if (Input.GetKey (KeyCode.D)) {
                 transform.Translate(Vector3.right * waterspeed * Input.GetAxis("Horizontal"));
         }
 
 //            if (Input.GetAxis("Horizontal") != 0)
 //            {
 //                transform.Translate(Vector3.right * waterspeed * Input.GetAxis("Horizontal"));
 //        //    transform.Translate(thecam.transform.right * waterspeed * Input.GetAxis("Vertical"));
 //        }
 
 
 
         // This ensures that we don't move faster going diagonally
         if(direction.magnitude > 1f) {
             direction = direction.normalized;
         }
 
     }
 
     
     // FixedUpdate is called once per physics loop
     // Do all MOVEMENT and other physics stuff here.
     void FixedUpdate () {
         
         // "direction" is the desired movement direction, based on our player's input
         Vector3 dist = direction * waterspeed * Time.deltaTime;
 
         // Apply the movement to our character controller (which handles collisions for us)
         cc.Move( dist );
     }
 }

Thanks!

Comment
Add comment · Show 2
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 Fewpwew130 · Dec 10, 2015 at 07:16 AM 0
Share

Here is the working script, if anyone is interested:

 using UnityEngine;
 using System.Collections;
 
 public class watermovement : $$anonymous$$onoBehaviour {
 
     // This component is only enabled for "my player" (i.e. the character belonging to the local client machine).
     // Remote players figures will be moved by a NetworkCharacter, which is also responsible for sending "my player's"
     //location to other computers.
     
     public float waterspeed = 10f;        // The speed at which I run
     public float waterjumpSpeed = 6f;    // How much power we put into our jump. Change this to jump higher.
     
     // Booking variables
     Vector3 direction = Vector3.zero;    // forward/back & left/right
     float   verticalVelocity = 0;        // up/down
     
     CharacterController cc;
     Animator anim;
 
     public Camera thecam;
 
 
     // Use this for initialization
     void Start () {
         cc = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         
         // WASD forward/back & left/right movement is stored in "direction"
 //        direction = transform.rotation * new Vector3(Input.GetAxis("Horizontal") ,0, Input.GetAxis("Vertical") );
 
     //    direction = transform.rotation * new Vector3(Input.GetAxis("Horizontal") ,0,Input.GetAxis("Vertical") );
         direction = thecam.transform.rotation * new Vector3(Input.GetAxis("Horizontal") ,0,Input.GetAxis("Vertical") );
 
 
 
 
 
         // This ensures that we don't move faster going diagonally
         if(direction.magnitude > 1f) {
             direction = direction.normalized;
         }
 
     }
 
     
     // FixedUpdate is called once per physics loop
     // Do all $$anonymous$$OVE$$anonymous$$ENT and other physics stuff here.
     void FixedUpdate () {
         
         // "direction" is the desired movement direction, based on our player's input
         Vector3 dist = direction * waterspeed * Time.deltaTime;
 
         // Apply the movement to our character controller (which handles collisions for us)
         cc.$$anonymous$$ove( dist );
     }
 }
avatar image LazyElephant · Dec 13, 2015 at 12:24 AM 0
Share

If the answers provided were correct, could you please accept them by clicking the check mark next to them? Thanks

3 Replies

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

Answer by LazyElephant · Dec 10, 2015 at 05:19 PM

What's happening with your horizontal motion is: when you press 'A', the GetAxis() function is returning a negative number. You then multiply this by Vector3.left which is (-1,0,0) and the result is a positive number which is the same as if you had just pressed 'D'. If you change the code as follows, you should be able to move left and right.

  if (Input.GetKey (KeyCode.D) || Input.GetKey(KeyCode.A)) {
          transform.Translate(Vector3.right * waterspeed * Input.GetAxis("Horizontal"));
  }

As for the vertical direction, I'm not sure if about the answer since it appears you're using the Vector3.forward direction as your vertical direction? I would have asssumed it would be Vector3.up, in which case the solution would be the same as above.

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
1

Answer by jmonasterio · Dec 10, 2015 at 05:24 PM

For the LEFT/RIGHT problem, I would focus on lines 57 and 61, for example:

  transform.Translate(Vector3.left * waterspeed * Input.GetAxis("Horizontal"));

The Input.GetAxis("Horizontal") will have a sign (positive or negativate) already. So that will handle the LEFT/RIGHT for you. So both lines should use the same direction: Like "Vector3.left". Otherwise you're double flipping the direction and you'll always go RIGHT regardless of whether D or A is clicked.

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 Fewpwew130 · Dec 10, 2015 at 06:00 PM

Thank you guys for answers! These answers really helped. I also wanted to say that the community here, at Unity is just wonderful - patient and eager to assist. I added a comment with a working script to the main question. Thank you LazyElephant and jmonasterio for help, - I upvoted both of your answers.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

36 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

Related Questions

"Swimming" Physics with Rigidbodies 2 Answers

my first person controller dosent work properly 2 Answers

Network? controles backwards? 1 Answer

Keyboard / Mouse control for FPS 0 Answers

How can you make a FPS controller camera "fly"? 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