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 Misthra-Games · May 01, 2016 at 11:45 PM · rotationmovementplayermouselook

Player not rotating with camera

Hey, so I was writing a mouse look and player movement script to replace the one that comes with unity, but for some reason, the character rotation is not working.

MuseLook Script :

 using UnityEngine;
 using System.Collections;
 
 public class MouseLook : MonoBehaviour {
     private float lookSensitivity = 5f;
     private float xRotation;
     private float yRotation;
     private float currentXRotation;
     private float currentYRotation;
     private float xRotationV;
     private float yRotationV;
     private float smoothnessFactor = .1f;
     CursorLockMode wantedMode;
 
     // Use this for initialization
     void Start () {
         wantedMode = CursorLockMode.Confined;    
     }
 
     public float returnXRot () {
         return currentXRotation;
     }
 
 
     public float returnYRot () {
         return currentYRotation;
     }
     
     // Update is called once per frame
     void Update () {
         Cursor.lockState = wantedMode;
 
         xRotation -= Input.GetAxis ("Mouse Y") * lookSensitivity;
         yRotation += Input.GetAxis ("Mouse X") * lookSensitivity;
     
         xRotation = Mathf.Clamp (xRotation, -90, 90);
 
         currentXRotation = Mathf.SmoothDamp (currentXRotation, xRotation, ref xRotationV, smoothnessFactor);
         currentYRotation = Mathf.SmoothDamp (currentXRotation, yRotation, ref yRotationV, smoothnessFactor);
 
         transform.rotation = Quaternion.Euler (currentXRotation, currentYRotation, 0);
     }
 
 }


PlayerMovement Script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     private float speed = 10f;
     private float jumpForce = 8f;
     private float gravity = 30f;
     private Vector3 moveDir = Vector3.zero;
     public GameObject playerCamera;
 
 
     void Start () {
         playerCamera = GameObject.Find ("playerCamera");
 
     }
 
     void Update () {
         CharacterController controller = gameObject.GetComponent<CharacterController> ();
 
         if (controller.isGrounded) {
             moveDir = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
             moveDir = transform.TransformDirection (moveDir);
             moveDir *= speed;
             if (Input.GetButtonDown ("Jump")) {
                 moveDir.y = jumpForce;
             }
         }
 
 
         moveDir.y -= gravity * Time.deltaTime;
         controller.Move (moveDir * Time.deltaTime);
 
         transform.rotation = Quaternion.Euler (0, playerCamera.transform.rotation.y, 0);
     }
 }
 

Any help would be much apreciated

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

Answer by incorrect · May 01, 2016 at 11:48 PM

I suppose first script is attached to the camera so it affects camera's rotation and does nothing with player object. I'd suggest to use currentYRotation to rotate player as well. If your camera is a child of player object, you can rotate horizontally the player object instead of a camera.

Comment
Add comment · Show 4 · 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 Misthra-Games · May 02, 2016 at 05:00 AM 0
Share

Thanks! I just set it to rotate my player ins$$anonymous$$d! Now can you tell me why it glitches so badly when i look to the left and right??

avatar image Misthra-Games · May 02, 2016 at 05:11 AM 0
Share

wait Never$$anonymous$$d, that didnt rotate the player either

avatar image Misthra-Games · May 02, 2016 at 05:14 AM 0
Share

Oh now it works! I just made BoTH of them turn on the Y. Im guessing thats what you meant :/

avatar image incorrect Misthra-Games · May 02, 2016 at 10:38 AM 0
Share

Actually if your camera is a child of a player object, it will be rotated with player object. But keep in $$anonymous$$d that in this case you'd better use transform.localRotation and transform.localPosition ins$$anonymous$$d.

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

58 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

Related Questions

How to disable Mouselook with the new Unity 5? 1 Answer

Rotation for movement cannot be set in script? 1 Answer

Checking GameObject Position on Andriod 1 Answer

How to create a player movement with Rotation? 1 Answer

Issues with camera and player rotation scripts for basic FPS controller. 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