• 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 /
  • Help Room /
avatar image
0
Question by dmitry-kozyr · Mar 20, 2016 at 01:51 PM · c#unity 5

Player movement script like in FPS

I have 2 scripts, which make player move like in the FPS game. But it don't move to that direction, to which player are looking - it always move to the same direction, regardless of the direction of the camera..

mouselook.cs

 float yRotation;
 float xRotation;
 float lookSensitivity = 5;
 float currentXRotation;
 float currentYRotation;
 float yRotationV;
 float xRotationV;
 float lookSmoothnes = 0.1f; 
 
 void Update ()
 {
     yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
     xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
     xRotation = Mathf.Clamp(xRotation, -80, 100);
     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothnes);
     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothnes);
     transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
 }

playermovement.cs

 public float walkSpeed = 6.0F;
 public float jumpSpeed = 8.0F;
 public float runSpeed = 8.0F;
 public float gravity = 20.0F;
 
 private Vector3 moveDirection = Vector3.zero;
 private CharacterController controller;
 
 void Start()
 {
     controller = GetComponent<CharacterController>();
 }
 
 void Update()
 {
     if (controller.isGrounded)
     {
         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= walkSpeed;
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;
     }
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }


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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by nathanlink169 · May 28, 2017 at 07:35 PM

Your issue lies in this line:

moveDirection = transform.TransformDirection(moveDirection);

Rather than pass in moveDirection to the function TransformDirection, you want to pass in the forward vector for camera.

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 jose9909 · May 28, 2017 at 03:14 PM

if you put both codes in a same script, it basically works, the only issue is that the player LITERALLY moves to the direction of the camera, so i guess you would have to implement some dampening lines after the vector3 part

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 wafflking · Feb 03, 2020 at 02:18 AM

For the camera part i did some editing to fix it for the new unity versions but im getting a error saying No overload for method 'SmoothDamp' takes 3 arguments

using UnityEngine; using System.Collections; using System.Collections.Generic; public class mouselook : MonoBehaviour { float yRotation; float xRotation; float lookSensitivity = 5f; float currentXRotation; float currentYRotation; float yRotationV; float xRotationV;

 void Update()
 {
     yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
     xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
     xRotation = Mathf.Clamp(xRotation, -80, 100);
     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV);
     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV);
     transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
 }

}***

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

131 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

Related Questions

transform.Rotate having no effect 0 Answers

Score Reset on 2nd Play 1 Answer

Unity MVC animation coroutine 0 Answers

uNet hitboxes and movement. 0 Answers

animations not working on multi play mode? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges