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 Hunter4408 · May 28, 2016 at 07:06 PM · cameracharactercontrollercontroller

WASD Controls not in-sync with camera?

I'm making a game with a first-person view, and I decided not to use the prefab first-person controller and try to create it all from scratch. I finally got the camera to move when I move the mouse, but the WASD controls don't seem to be correct. I can go forward, backward, and sideways with WASD but in many games they change with the camera. In the game for example, when I first start up and go forward with the W/forward-arrow key, and I turn the camera in the opposite direction, I can't use W to go into that direction. I have to use the S key to go back. This is the same with all directions. I just need to know what function I need so that the direction I'm facing with the camera, the controls become oriented with that direction. I've been looking, I'm not sure if there are any other up-to-date questions about this.

 using UnityEngine;
 using System.Collections;
 
 public class WalkScript : MonoBehaviour {
     private Transform player;
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     float xAxisValue = Input.GetAxis("Horizontal");
     float zAxisValue = Input.GetAxis("Vertical");
    
 void Update()
     {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded)
         {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
 
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
         }
     }
 

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 Kryzarel · May 28, 2016 at 09:27 PM 1
Share

You should post your code along with your question. That would give us a better idea of what you are doing and help us figure out why it's not doing what you want it to.

Anyway, you said you're already rotating the camera as the mouse moves, but are you also rotating the player? The player is probably moving in the direction it is facing and not the direction the camera is facing.

avatar image Hunter4408 Kryzarel · May 30, 2016 at 09:00 PM 0
Share

I'm sorry that I didn't reply yesterday. I'm completely new to this, I have had no knowledge whatsoever with C# before so I've been using tutorials. I will add the script to the question.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DiGiaCom-Tech · May 28, 2016 at 07:45 PM

@Hunter4408 ... Is the Camera a child of the Player? If it is NOT then you must write code to update the Camera's position/rotation so that it follows the Player as you intend. The easiest solution is to place the Camer as a child of the Player and set it's position/rotation relative to the Player as you like (i.e. looking at the Player's back for 3rd person or inside the Player's head for 1st person). This way it becomes part of the Player and as the Player moves and turns the Camera moves and turns in sync.

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 Hunter4408 · May 30, 2016 at 09:08 PM 0
Share

I'm 99.9% sure the camera is a child of the player, and they are both aligned. If you mean that I have to hit a tab to see the Camera on the hierarchy, then it is a part of the Player.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I limit the rotation on the Y axis so the player cant spin the camera 360 in an FPS game? 0 Answers

How to add controller input to keyboard input 1 Answer

Change position of camera on scene load? 1 Answer

Mobile Character controller Moving in world space 1 Answer

Character controller without Deltatime 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