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 /
  • Help Room /
avatar image
0
Question by the47thhokage · Jan 12, 2018 at 03:14 AM · player movementcamera rotatecamera-lookfps controller

i am tying to build a fps controller from scratch and cant seem to get him to look up or down

[RequireComponent(typeof(Rigidbody))] public class Movement : MonoBehaviour { [SerializeField] private Camera Cam;

 Rigidbody rb;
 private Vector3 velocity = Vector3.zero;
 private Vector3 rotation = Vector3.zero;
 //private Vector3 camreaRotation = Vector3.zero;
 private float camreaRotationX = 0f;
 private float currentcamreaRotationX = 0f;

 [SerializeField]
 private float cameraRotationLimit = 85f; 

 private void Start()
 {
     rb = GetComponent<Rigidbody>();
 }
 // Gets a movement vector
 public void Mov(Vector3 _velocity)
 {
     velocity = _velocity;
 }
 //get a rotational vector
 public void Rotate(Vector3 _rotation)
 {
     rotation = _rotation;
 }

 //get a rotational vector for cam
 public void RotateCamera(float _camreaRotationX)
 {
     camreaRotationX = _camreaRotationX;
 }

 //runs all Physics
 private void FixedUpdate()
 {
     PerformMovement();
     PerformRotation();
 }

 void PerformMovement()
 {
     if (velocity != Vector3.zero)
     {
         rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
     }

 }

 void PerformRotation()
 {
     rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
     if (Cam != null)
     {
         // Set our rotation and clamp it
         currentcamreaRotationX -= camreaRotationX;
         currentcamreaRotationX = Mathf.Clamp(currentcamreaRotationX, -cameraRotationLimit, cameraRotationLimit);

         //Apply our rotation to the transform of our camera
         Cam.transform.localEulerAngles = new Vector3(currentcamreaRotationX, 0f, 0f);

         //Cam.transform.Rotate(-camreaRotation);
     }
 }

}

Player Controller

[RequireComponent(typeof(Movement))] public class PlayerController : MonoBehaviour { [SerializeField] private float Movespeed = 10.0f; [SerializeField] private float LookSens = 10.0f;

 private Movement Motor;

 private void Start()
 {
     Motor = GetComponent<Movement>();
 }

 private void Update()
 {
     //lock cursor to center of screen press Esc to escape  
     Cursor.lockState = CursorLockMode.Locked;

     //calculate movement Velocity as 3D vector
     float xMov = Input.GetAxisRaw("Horizontal");
     float zmov = Input.GetAxisRaw("Vertical");

     Vector3 MovHoriz = transform.right * xMov;
     Vector3 MovVert = transform.forward * zmov;
     // final movement Vector
     Vector3 Velocity = (MovHoriz + MovVert).normalized * Movespeed;

     //Apply Movement
     Motor.Mov(Velocity);

     //Calculate rotation as 3D Vector (turning arround)
     float _yrot = Input.GetAxisRaw("Mouse X");

     Vector3 _rotation = new Vector3(0f, _yrot, 0f) * LookSens;
     //Apply rotation 
     Motor.Rotate(_rotation);


     //Calculate camrea rotation as 3D Vector (turning arround)
     float _xrot = Input.GetAxisRaw("Mouse Y");

     float _cameraRotationX = _xrot * LookSens;
     //Vector3 _camrearotation = new Vector3(0f, _xrot, 0f) * LookSens;
     //Apply rotation 
     Motor.RotateCamera(_cameraRotationX);
 }

}

,[RequireComponent(typeof(Rigidbody))] public class Movement : MonoBehaviour { [SerializeField] private Camera Cam;

 Rigidbody rb;
 private Vector3 velocity = Vector3.zero;
 private Vector3 rotation = Vector3.zero;
 //private Vector3 camreaRotation = Vector3.zero;
 private float camreaRotationX = 0f;
 private float currentcamreaRotationX = 0f;

 [SerializeField]
 private float cameraRotationLimit = 85f; 

 private void Start()
 {
     rb = GetComponent<Rigidbody>();
 }
 // Gets a movement vector
 public void Mov(Vector3 _velocity)
 {
     velocity = _velocity;
 }
 //get a rotational vector
 public void Rotate(Vector3 _rotation)
 {
     rotation = _rotation;
 }

 //get a rotational vector for cam
 public void RotateCamera(float _camreaRotationX)
 {
     camreaRotationX = _camreaRotationX;
 }

 //runs all Physics
 private void FixedUpdate()
 {
     PerformMovement();
     PerformRotation();
 }

 void PerformMovement()
 {
     if (velocity != Vector3.zero)
     {
         rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
     }

 }

 void PerformRotation()
 {
     rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
     if (Cam != null)
     {
         // Set our rotation and clamp it
         currentcamreaRotationX -= camreaRotationX;
         currentcamreaRotationX = Mathf.Clamp(currentcamreaRotationX, -cameraRotationLimit, cameraRotationLimit);

         //Apply our rotation to the transform of our camera
         Cam.transform.localEulerAngles = new Vector3(currentcamreaRotationX, 0f, 0f);

         //Cam.transform.Rotate(-camreaRotation);
     }
 }

}

player controller

[RequireComponent(typeof(Movement))] public class PlayerController : MonoBehaviour { [SerializeField] private float Movespeed = 10.0f; [SerializeField] private float LookSens = 10.0f;

 private Movement Motor;

 private void Start()
 {
     Motor = GetComponent<Movement>();
 }

 private void Update()
 {
     //lock cursor to center of screen press Esc to escape  
     Cursor.lockState = CursorLockMode.Locked;

     //calculate movement Velocity as 3D vector
     float xMov = Input.GetAxisRaw("Horizontal");
     float zmov = Input.GetAxisRaw("Vertical");

     Vector3 MovHoriz = transform.right * xMov;
     Vector3 MovVert = transform.forward * zmov;
     // final movement Vector
     Vector3 Velocity = (MovHoriz + MovVert).normalized * Movespeed;

     //Apply Movement
     Motor.Mov(Velocity);

     //Calculate rotation as 3D Vector (turning arround)
     float _yrot = Input.GetAxisRaw("Mouse X");

     Vector3 _rotation = new Vector3(0f, _yrot, 0f) * LookSens;
     //Apply rotation 
     Motor.Rotate(_rotation);


     //Calculate camrea rotation as 3D Vector (turning arround)
     float _xrot = Input.GetAxisRaw("Mouse Y");

     float _cameraRotationX = _xrot * LookSens;
     //Vector3 _camrearotation = new Vector3(0f, _xrot, 0f) * LookSens;
     //Apply rotation 
     Motor.RotateCamera(_cameraRotationX);
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

122 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

Related Questions

Tilt camera left or right on keyboard input,Tilt camera when player moves left or right 0 Answers

Camera spawn rotate with object 0 Answers

How do I do my camera player look at an specific direction when key pressed 0 Answers

How to configure my camera ?? 0 Answers

(NEED HELP!) Camera controlled by mouse Y, player controlled by mouse X 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