Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GhostDre · May 11, 2014 at 06:58 AM · cameramovementthird-person

Movement with Camera

Hi everyone, I have a problem with my movement or camera or maybe both. I made two seperate script which are PlayerMovement which has the players movement in C# and a MouseLook which enables you to look around in 360 degrees also in C#. Now everything works expect when I move forward and lets say I turn my camera to the right the player starts to strafe right and if I turn the camera to the right again he is now moving backwards. By the way this is a Third Person Game. Any Ideas on how I can fix this?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float speed = 5.0f;
     public float jumpSpeed = 5.0f;
     public float gravity = 20.0f;
     public float run = 2.0f;
     public float crouchSpeed = 0.5f;
     private Vector3 moveDirection = Vector3.zero;
 
     void Start()
     {
                 transform.position = new Vector3(0,1.7f,0);
         }
 
     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;
                             if (Input.GetKey(KeyCode.LeftShift))
                                 moveDirection *= run;
                                 if (Input.GetButton("Crouch"))
 
                                     //transform.position.y -= 1.5;
                                     //transform.localScale.y -= new Vector3(0,.5f,0);
                                     moveDirection *= crouchSpeed ;
                                     
                 }
 
                 moveDirection.y -= gravity * Time.deltaTime;
                 controller.Move (moveDirection * Time.deltaTime);
         }
 }


 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Mouse Look")]
 public class MouseLook : MonoBehaviour {
 
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 15F;
     public float sensitivityY = 15F;
 
     public float minimumX = -360F;
     public float maximumX = 360F;
 
     public float minimumY = -60F;
     public float maximumY = 60F;
 
     float rotationX = 0F;
     float rotationY = 0F;
     
     Quaternion originalRotation;
 
     void Update ()
     {
         if (axes == RotationAxes.MouseXAndY)
         {
             // Read the mouse input axis
             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
 
             rotationX = ClampAngle (rotationX, minimumX, maximumX);
             rotationY = ClampAngle (rotationY, minimumY, maximumY);
             
             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
             
             transform.localRotation = originalRotation * xQuaternion * yQuaternion;
         }
         else if (axes == RotationAxes.MouseX)
         {
             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
             rotationX = ClampAngle (rotationX, minimumX, maximumX);
 
             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
             transform.localRotation = originalRotation * xQuaternion;
         }
         else
         {
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = ClampAngle (rotationY, minimumY, maximumY);
 
             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
             transform.localRotation = originalRotation * yQuaternion;
         }
     }
     
     void Start ()
     {
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
         originalRotation = transform.localRotation;
     }
     
     public static float ClampAngle (float angle, float min, float max)
     {
         if (angle < -360F)
             angle += 360F;
         if (angle > 360F)
             angle -= 360F;
         return Mathf.Clamp (angle, min, max);
     }
 }

 
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 Eluate · May 11, 2014 at 11:35 AM 0
Share

Transform.right and Transform.forward is your friend.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hachley · May 11, 2014 at 01:46 PM

What you need to do is that if you move to the right(with your mouse) then also rotate the face of your players body to the right. What you have now is that if you hold W and look straight with your mouse, then the players body moves forward, but once you turn your camera 90 degrees to the right, you only rotate the camera but not the whole players body. If you would to rotate the body at the same direction where you rotate the camera, then the "front" of the body would always be where you are facing with your camera.

If you look at the default first-person-controller that unity provides, then go inside the mouse-look script and find the bit that tells you that it rotates the body every time you move the camera. They //comment every section of the script.

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 GhostDre · May 11, 2014 at 05:18 PM

Where would I find the FPS Controller. And if so can I see an example?

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 Hachley · May 11, 2014 at 06:09 PM 0
Share

I suggest you see some tutorials and lessons that Unity offers here, otherwise you would know about the FPS Controller. Anyway, you can right-click on your project window, then click "Import Package">"Character Controller", after that you can find the character controller in the folders and then drag it into the scene, you will see a capsule player object in the game. Also, if you run the game now, then you will be able to play with the imported character right away(unless you have another in the scene). If you go into inspector of the character controller, you will see many scripts attached to it. Once you click on the "$$anonymous$$ouseLook" script, inside you will find the code corresponding to the mouse look movement that also only rotates the characters body right/left(according to your mouse) but not up/down, so that your character doesn't tilt his body.

Check it out yourself, there's a lot of useful stuff there, and I'd suggest you start with that character controller and just modify it to your needs.

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

22 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

Related Questions

How to make my player walk according to the cameras direction? 2 Answers

Third Person Camera Help? 0 Answers

Constraining a camera movement 0 Answers

How to make my player look in the direction of its movement? 3 Answers

Moving (teleporting) player and camera when they enter a certain area 5 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