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 Ordance · Jan 18, 2020 at 10:22 PM · movementfpscharactercontrollercharacter controllerfps controller

How can I make my FPS player move towards the direction it is facing

I am trying to make an FPS controller using Character Controller, I can get it to move, but the problem is that it is moving on global space. I watched a lot of videos on youtube but nobody is explaining how the code works, so can someone tell me how can I make it move towards the direction where it is facing and how the code works.

Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {    
 
     private CharacterController characterController;
     public GameObject camera;
 
     [SerializeField] private float moveSpeed = 5f;
     [SerializeField] private float mouseSensitivity = 2f;
 
     void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
         characterController = GetComponent<CharacterController>();
     }
 
     void Update()
     {
         Move();
         RotatePlayer();
     }
 
     private void Move()
     {
         float moveLeftAndRight = Input.GetAxisRaw("Horizontal");
         float moveForwardAndBackward = Input.GetAxisRaw("Vertical");
 
         Vector3 moveDirection = new Vector3(moveLeftAndRight, 0f, moveForwardAndBackward) * moveSpeed * Time.deltaTime;
 
         characterController.Move(moveDirection);
     }
 
     private void RotatePlayer()
     {
         float mouseX = Input.GetAxisRaw("Mouse X");
         float mouseY = Input.GetAxisRaw("Mouse Y");
 
         float mouseXLook = mouseX * mouseSensitivity;
         float mouseYLook = mouseY * mouseSensitivity;
 
         transform.Rotate(0, mouseXLook, 0f);
         camera.transform.Rotate(-mouseYLook, 0f, 0f);
     }
 }
 



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
1
Best Answer

Answer by Esteem · Jan 18, 2020 at 11:15 PM

to set the direction in relation to the object that the PlayerController sits on (the player object), you want to add your moveLeftAndRight and moveForwardAndBackward to the forward and right vectors of your player object.

instead of this:

 Vector3 moveDirection = new Vector3(moveLeftAndRight, 0f, moveForwardAndBackward) * moveSpeed * Time.deltaTime;


do this:

 Vector3 moveDirection = 
     (transform.forward * moveForwardAndBackward + transform.right * moveLeftAndRight) 
     * moveSpeed * Time.deltaTime;



Comment
Add comment · Show 3 · 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 Ordance · Jan 18, 2020 at 11:26 PM 0
Share

Thank you for helping me, and because I am a beginner I'm a little bit confused, why should I use "+" ins$$anonymous$$d of "," since it is a vector3 and it needs 3 values

avatar image Esteem Ordance · Jan 19, 2020 at 10:53 AM 1
Share

we're setting the value of Vector3 to the value of (Vector3 * float) + (Vector3 * float) * float * float

You can add two Vector3s together to get another Vector3 and you can multiple a VEctor3 by a float.

what you can't do is multiply a Vector3 by a Vector3 (at least by default)

avatar image Ordance Esteem · Jan 19, 2020 at 12:05 PM 0
Share

Thank you for the explanation

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

223 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Carry forward velocity against mouse movements 1 Answer

How do I make the default fps character controller not stick to the mesh it is standing on? 0 Answers

How do I prevent my fps player from flying? 1 Answer

Why does my player just fall through the floor right when I press play? 1 Answer

Player Falls Much Slower While Moving 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