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 /
This question was closed Sep 14, 2015 at 01:13 PM by MrVersus for the following reason:

The question is answered, right answer was accepted ! Please check not only the answered given , but read the comments bellow of the answer for more information about the situation!

avatar image
0
Question by MrVersus · Sep 13, 2015 at 02:02 PM · c#cameracamera-movementcamera-lookcamera rotation

My camera rotate but my player dont. How Ive to do so?

Hello C sharp and Unity Dev's,

So the problem is this, I can't rotate my character when I turn my camera. When I rotate 180º I press W and my Character go backwards (same for the other keys. )

PlayerMovement Script

 public GameObject Camera01;

 //Variables for Player Movement
 
 public float speedWalk = 2.0f; //Velocidade de Andar
 public float speedRun = 12.0f; //Velocidade de Correr
 public float speedCrch = 2.0f; //Velocidade de Agachado

    
 public Rigidbody rb;
 private float mass;
 
 void Start() 
 {
     Camera01 = GetComponent<GameObject>();
     rb = GetComponent<Rigidbody>();
     rb.mass = mass;

 }

 void FixedUpdate() 
 {  
     //Char > Movement

     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
     
     Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);
 
     rb.velocity = movement * speedWalk;
     rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
   
     //Mouse look > Script


     transform.rotation = Quaternion.Euler(0, Camera01.GetComponent<MouseLook>().currentYRotation, Camera01.GetComponent<MouseLook>().currentYRotation);
 }

 public int tilt { get; set; }

MouseLook Script

 public GameObject Camera01;
 public float lookSensitivity = 5f;
 public float xRotation;
 public float yRotation;
 public float currentXRotation;
 public float currentYRotation;
 public float xRotationV;
 public float yRotationV;

 public float lookSmoothDamp = 0.1f;

 //Init
 void Start()
 {
       
 }    
 //FixedUpdate
 void FixedUpdate()
 {
     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, lookSmoothDamp);
     currentYRotation = Mathf.SmoothDamp (currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);

     transform.rotation = Quaternion.Euler(0, currentYRotation, currentXRotation);       
 }


More print screens: ( @pako )

Player Inspector

Camera Inspector

Hierarchy

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 pako · Sep 12, 2015 at 06:26 PM 1
Share

Please clarify if Player$$anonymous$$ovement Script is attached to a Player GameObject and $$anonymous$$ouseLook Script is attached on Camera GameObject. Also it would be even more helpful if you edit your question and include screenshots of the Hierarchy and Inspector, showing the Player and Camera GameObjects.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by pako · Sep 13, 2015 at 04:08 PM

I edited your scripts in order to make them work. I included a lot of comments to show you what I changed. Mainly, in your original scripts you had some axes the wrong way round, and also I make it work without a rigidbody. I don't know why you want to use these scripts (even my version that work). It would be better if you used the "First Person Character Controller" from Unity's "Standard Assets". Anyway, here are the corrected scripts:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public GameObject Camera01;  //Camera01 MUST be dragged and dropped in this Inspector field
     //Variables for Player Movement
 
     public float speedWalk = 2.0f; //Velocidade de Andar
     public float speedRun = 12.0f; //Velocidade de Correr
     public float speedCrch = 2.0f; //Velocidade de Agachado
 
     private Rigidbody rb;
     private float mass;
 
     public int tilt { get; set; }
 
     private MouseLook mouseLook;  //variable to cached the MouseLook script for better performance
 
     void Start()
     {
         //Camera01 = GetComponent<GameObject>();
         rb = GetComponent<Rigidbody>();  //Rigidbody not used in this version of the script, so if it's not needed it can be deleted
 
         //rb.mass = mass; // assignement is made the wrong way around => rb.mass should be assigned to mass (see below
         mass = rb.mass;  //however mass is not (yet at least)used anywhere in this script, so if it's not needed it can be deleted
 
         //cache MouseLook script
         mouseLook = Camera01.GetComponent<MouseLook>();
     }
 
     void FixedUpdate()
     {
 
         //Char > Movement
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         //Vector3 movement = new Vector3(moveHorizontal, 0.0f , moveVertical);
 
         //rb.velocity = movement * speedWalk;
         //rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
 
         //Rotate Left/Right towards the camera rotation, in order to always face forwards
         //Tilting the player's "body" Up/Down where "his eyes"are looking is not really necessary
         //So, rotating around the x-axis is not really necessary
         //NOTE: In the original script the player was rotating by mistake around the z-axis
         transform.rotation = Quaternion.Euler(0f, mouseLook.currentYRotation, 0f);
 
         transform.position += (transform.forward * moveVertical * speedWalk) + (transform.right * moveHorizontal * speedWalk);
     }
     
 }
 
 
 using UnityEngine;
 using System.Collections;
 
 public class MouseLook : MonoBehaviour {
 
     //public GameObject Camera01;   //not needed can be deleted
     public float lookSensitivity = 5f;
     public float xRotation;
     public float yRotation;
     public float currentXRotation;
     public float currentYRotation;
     public float xRotationV;
     public float yRotationV;
     public float lookSmoothDamp = 0.1f;
 
     //Init
     void Start()
     {
 
     }
 
     //FixedUpdate
     void FixedUpdate()
     {
         xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;  //controls up/down look => mouse forward/backwards corresponds to camera rotation about x-axis
         yRotation += Input.GetAxis("Mouse X") * lookSensitivity; //controls left/right look => mouse left/right corresponds to camera rotation about y-axis
         xRotation = Mathf.Clamp(xRotation, -90, 90);
         currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
         currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
 
         //rotates camera - player should rotate with camera rotation ONLY left/right
         //i.e. camera up/down should correspond only to player eye movement, not player body movement
         //So, PlayerMovement script should check camera's rotation in order to rotate player accordingly
         //So, PlayerMovement script MUST have a reference to the Camera GameObject in order to know its rotation
         transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
     }
 }
 
 
Comment
Add comment · Show 2 · 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 MrVersus · Sep 13, 2015 at 07:02 PM 0
Share

Woaw thanks. $$anonymous$$y point on making my own script is for learning proposes ( C sharp ) :p ! But I can still use with rigid body, right? Because I want this "Player Object " interact " with the world around! And my axes are twisted because of something I did on Input (x , but now I reset them and should work with this re-new script.

I really appreciate your help , I'll put them now and see what happens, then I'll update you here or by P$$anonymous$$. And sorry for my English. @pako

avatar image pako · Sep 13, 2015 at 07:46 PM 1
Share

@$$anonymous$$rVersus you are welcome! I suspected you did it for learning purposes, this is why I included so many comments, so you understand the changes I made.

Of course you can have a Rigidbody on the player for other interactions with game world. I tried very hard to make the movement work properly using the rigidbody.velocity for the movement, as was the original script, but I didn't succeed. $$anonymous$$aybe it would work if I made it more complicated, but I preferred to simplify it, ins$$anonymous$$d of making it more complicated.

I tested it and it works fine. Some details:

  • $$anonymous$$ouseLook is not need on Player$$anonymous$$ovement GameObject. I have deleted it in my test project.

  • $$anonymous$$ouseLook script only on Camera01 GameObject

  • In Player$$anonymous$$ovement script, you must drag-n-drop the Camera01 GameObject inside the "Camera 01" field in the Inspector, otherwise player rotation will not work

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Camera Jitters When Displacing and Rotating Smoothly 0 Answers

,how to set max rotation for camera in third person game 0 Answers

Make Camera follow rotation of 2 Players 1 Answer

Sit Down First person 0 Answers

Scripting beginner - how can I make the camera follow the character but still face the same direction? 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