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 BobMen645 · May 12, 2020 at 10:29 AM · cameracamera-movementmovement scriptcamera-look

Help My movement is broken!!

I'm making a FPS and i need help with the movement i already have a script for locking my mouse and making my player follow it but the movement it broken if i look one way it doesn't work like it looks that way but it doesn't move that way when I press W Plz help!!

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 ADiSiN · May 12, 2020 at 04:50 PM 0
Share

Hi, you described your issue quite well, but we cannot help without seeing code that you implement for moving, can you attach your code so we could check if there is any issue in there?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BobMen645 · May 12, 2020 at 10:09 PM

Sure thing here you go using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Movement : MonoBehaviour { CharacterController characterController;

 public float speed = 6.0f;
 public float jumpSpeed = 8.0f;
 public float gravity = 20.0f;

 private Vector3 moveDirection = Vector3.zero;

 void Start()
 {
     characterController = GetComponent<CharacterController>();
 }

 void Update()
 {
     if (characterController.isGrounded)
     {
        

         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
         moveDirection *= speed;

         if (Input.GetButton("Jump"))
         {
             moveDirection.y = jumpSpeed;
         }
     }

     
     moveDirection.y -= gravity * Time.deltaTime;

    
     characterController.Move(moveDirection * Time.deltaTime);
 }

}

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 ADiSiN · May 13, 2020 at 01:21 AM

Thank you for showing your code.

Take a look at this one:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Movement : MonoBehaviour
 {
     CharacterController characterController;
 
     public float speed = 6.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
     private Vector3 moveDirection = Vector3.zero;
     void Start()
     {
         characterController = GetComponent<CharacterController>();
     }
     void Update()
     {
         if (characterController.isGrounded)
         {
             float xDir = Input.GetAxis("Horizontal");
             float zDir = Input.GetAxis("Vertical");
             moveDirection = transform.right * xDir + transform.forward * zDir;
             moveDirection.y = 0;        // Otherwise we will fly towards camera direction if it points upwards
 
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
             {
                 moveDirection.y = jumpSpeed;
             }
         }
 
         moveDirection.y -= gravity * Time.deltaTime;
 
         characterController.Move(moveDirection * Time.deltaTime);
     }
 }

Why in your code you didn't move towards forward direction of your view is because you never used it in code.

In the code abow we calculate Horizontal and Vertical input separately and then calculate overall moveDirection by multiplying input values to our right and forward direction of transform what makes it take into account our transform orientation. However, we shouldn't forget to reset moveDirection.y to 0, otherwise if we look slightly upwards or downwards it will fly into that direction.


Hope that helps.

Comment
Add comment · Show 2 · 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 BobMen645 · May 15, 2020 at 02:19 PM 0
Share

Thank you but there is one problem with the code can u help me So basically A is forward D is backwards W is left and S is right I'm stuck because the character controller is weird can you help I looked in the code and there is nothing there for the buttons so I'm stuck Thank you

avatar image ADiSiN BobMen645 · May 15, 2020 at 04:40 PM 0
Share

The "Horizontal" and "Vertical" Axises represent buttons a,d,left,right arrows and w,s,up,down arrows accordingly.

You can find it in Edit-Project Settings-Input $$anonymous$$anager.


Depends on how you want your gameplay to be controlled you can swap these two lines:

  float xDir = Input.GetAxis("Horizontal");
  float zDir = Input.GetAxis("Vertical");

To that:

  float xDir = Input.GetAxis("Vertical");
  float zDir = Input.GetAxis("Horizontal");

To change which buttons will represent forward which side direction.

Here is official doc with more information: https://docs.unity3d.com/ScriptReference/Input.GetAxis.html

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

199 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

Related Questions

Enable the OrbitCam script how can I make it will continue from the current camera view and position ? 0 Answers

Camera gets stuck when cursor is locked 0 Answers

Camera movement and gun follow in unity 1 Answer

Camera follow a sphere which look at another object on the ground 0 Answers

Free camera look question 2 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