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 VictorVaeVictis · May 17, 2014 at 06:58 AM · rotationfpsrotation axisrotation detection

How can I make player rotation control camera's x rotation and mouse control it's y rotation?

I have this bit of code

 using UnityEngine;
 using System.Collections;
 
 
 /// <summary>
 /// Camera script. This script is attached to the player and causes 
 /// the camera to follow the Player's position and rotation.
 /// </summary>
 /// 
 public class CameraScript : MonoBehaviour {
 
     private Camera myCamera;
 
     private Transform myTransform;
 
     // Use this for initialization
     void Start () {
 
         myCamera = Camera.main;
 
         myTransform = transform.FindChild("CameraHead");
     }
     
     // Update is called once per frame
     void Update () {
 
 
         myCamera.transform.position = myTransform.position;
     
         myCamera.transform.rotation = myTransform.rotation;
 
 
     }
 }

which makes the camera follow the player's position and rotation like a FPS. But what I need is for the Camera so only follow the players X rotation while I have the mouse control the cameras Y rotation. I've tried doing this.

 myCamera.transform.rotation.x = myTransform.rotation.x;

but that throws up errors. Any idea on how I can separate the X and Y rotation in this case?

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 robertbu · May 17, 2014 at 09:31 AM 0
Share

Just to make sure I've visualizing correctly, you want the camera to follow the player as the player is looking up or down. Correct?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · May 17, 2014 at 01:02 PM

This should get you startet. The script is attached to the camera which is a child of your player object.

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 rotationY = 0F;
 
     void Update ()
     {
         if(Screen.lockCursor == true) {
             if (axes == RotationAxes.MouseXAndY) {
                 float rotationX = new float();
                 
                 if(transform.parent != null && transform.parent.CompareTag("Player") == true) {
                     rotationX = transform.parent.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
                     
                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                     
                     transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);
                     transform.localEulerAngles = new Vector3(-rotationY, 0, 0);
                     
                 }
                 else if(transform.parent != null) {
                     rotationX = transform.parent.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
                     
                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                     
                     transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);
                     transform.localEulerAngles = new Vector3(-rotationY, 0, 0);
                 }
                 /*
                 else {
                     rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
                     
                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                     
                     transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
                 }
                 */
             }
             else if (axes == RotationAxes.MouseX) {
                 if(transform.parent != null) {
                     transform.parent.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
                 }
                 else {
                     transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
                 }
             }
             else {
                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                 
                 transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
             }
         }
     }
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 VictorVaeVictis · May 17, 2014 at 06:54 PM 0
Share

Thank you this is much much closer to what I want than the standard mouselook script, but there are still a couple problems I'm facing. $$anonymous$$y player is a rigidbody and with this script attached to the camera and my camera a child of the player. When I walk up ramps and stuff I get pulled back down and then the player continues to travel uncontrollably in the direction they fell down the ramp. This did not occur with the original mouselook script. Also, the biggest problem I am trying to solve is that my game has 6 players and each player (rigidbody) has it's own gravitational direction. So I have one player on the ceiling one player on the floor, one player and each wall. I have a script that rotates each player so they are facing upright on their particular wall and gravity applied in the proper direction for each player. Well when using $$anonymous$$ouseXandY with either mouselook scripts the player is turned back facing the global upright position as if every player stood on the floor. I am pretty sure this has to do with the Y Clamp and this line transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);, but I don't know how to modify it for my purposes. Any ideas?

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

21 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

Related Questions

i need to rotate cube in z axis or x axis one direction at time 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to Rotate 3D Object using Accelerometer? 1 Answer

unlocking weapon (fps) 1 Answer

Terrain with lots of objects 1 Answer


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