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 Rehtael · Dec 27, 2017 at 11:46 PM · c#camerarotation

Can't rotate a camera with another object

I'm making a piloting game akin to the classic X-wing and TIE fighter games, and am using a 360 controller to control the ship. Right stick controls the pitch and roll of the ship, and this works perfectly. I want the left stick to allow players to look left, right, and up in first person. I can get to the camera working to that extent, but when the camera is able to look in those directions, it no longer matches the rotation (x, y, z) of the ship.

Further explained: when the camera can't rotate via the left stick, it works fine, but when it can rotate via the left stick, it uses global rotation (I think that is what is happening)

I have the ship as a parent object ("ship"), and I have an empty game object ("holder"), with the camera ("cockpit") as a child of that empty game object.

I've been trying everything I can think of, can anyone help me figure this out?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerShipScript : MonoBehaviour
 {
 
     public float forwardSpeed;
     public float reverseSpeed;
 
     public float pitchSpeed;
     public float rollSpeed;
     public float yawSpeed;
     public float pitchTorque;
     public float rollTorque;
     public float yawTorque;
 
     public float pitchAxisVal;
     public float rollAxisVal;
     public float yawAxisVal;
 
     public float pitchCamVal;
     public float yawCamVal;
     public float rollCamVal;
 
     public float thrustSpeed;
     public float thrustRate;
     public float thrustMax;
     public float thrustMin;
 
     public Rigidbody ship;
 
     public Transform holder;
 
     public Camera cockpit;
 
     private float cockpitCameraAxis;
     
    
 
     private void Start()
     {
         thrustSpeed = 0.0f;
         thrustMax = 30.0f;
         thrustMin = 0.0f;
 
     }
 
     private void Update()
     {
 
         pitchCamVal = Input.GetAxis("LeftV");
         yawCamVal = Input.GetAxis("LeftH");
 
         pitchAxisVal = Input.GetAxis("RightV");
         rollAxisVal = Input.GetAxis("RightH");
         yawAxisVal = ship.rotation.y;
 
         //transform.Rotate(Vector3.left, (pitchAxisVal * pitchSpeed) * Time.deltaTime);
         //transform.Rotate(Vector3.forward, (rollAxisVal * -rollSpeed) * Time.deltaTime);
         ship.AddTorque(transform.right * -pitchAxisVal * pitchTorque);
         ship.AddTorque(transform.forward * -rollAxisVal * rollTorque);
 
         cockpit.transform.rotation = Quaternion.Euler((pitchCamVal * 90f) + ship.rotation.x, (yawCamVal * 90f) + ship.rotation.y, ship.rotation.z);
 
         if (Input.GetKeyDown(KeyCode.Joystick1Button0))
         {
             Debug.Log(holder.transform.rotation.x + " , " + holder.transform.rotation.y + " , " + holder.transform.rotation.z);
             Debug.Log(ship.transform.rotation.x + " , " + ship.transform.rotation.y + " , " + ship.transform.rotation.z);
         }
     }
 
     private void FixedUpdate()
     {
 
         ship.transform.Translate(Vector3.forward * thrustSpeed / 25.0f);
 
         if (Input.GetKey(KeyCode.Joystick1Button7) && (thrustSpeed < thrustMax))
         {
             thrustSpeed = thrustSpeed + thrustRate;
             Debug.Log("+: " + thrustSpeed);
         }
 
 
         if (Input.GetKey(KeyCode.Joystick1Button6) && (thrustSpeed > thrustMin))
         {
             thrustSpeed = thrustSpeed - thrustRate;
             Debug.Log("-: " + thrustSpeed);
         }
     }
 }
 
Comment
Add comment · Show 3
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 Larry-Dietz · Dec 28, 2017 at 04:52 AM 0
Share

Forgive me if I am a bit confused, but you are trying to rotate the camera, independent of the ship, in order to simulate looking around. But are concerned that the rotation no longer matches the ship? I thought that was what you were trying to accomplish? If you are trying to reorient it to the ship when they release the stick, just set the rotation to match the ship rotation with the stick is centered.

Sorry if I am misunderstanding something here. -Larry

avatar image Rehtael Larry-Dietz · Dec 28, 2017 at 05:04 AM 0
Share

I'd like the player to be able to turn their head in the cockpit, but currently, if the player is able to turn their head, the camera's rotation no longer matches the ship in its default state. I I'd like the camera to rotate on the parent's axis, so let's say that the ship starts at 0,0,0 rotation (Which it does) and I spin the ship around to face 180,90,0 rotation, the camera (when the player is not using the left stick) matches that 180,90,0 rotation. Then when the player moves the left stick to the left, the camera turns up to 90 degrees in that direction. I What is currently happening is if the player is able to turn the camera, the camera no longer stays with the ship, and the camera's focal point is still 0,0,0 regardless of which way the ship is pointed.

avatar image Rehtael Larry-Dietz · Dec 28, 2017 at 05:07 AM 0
Share

Side note, I currently have the head-turn camera to be a function of the left stick's -1 to 1 axis.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Larry-Dietz · Dec 28, 2017 at 05:11 AM

Sounds to me like simple resetting the camera's rotation to match the ship when the left stick is centered would solve the problem.

Or are you saying that when the code to handle the rotation is present, the camera stops matching the ship even when they are not using the stick?

Sorry for my confusion. Just trying to wrap my head around the problem before trying to figure out a solution for the wrong problem :)

-Larry

Comment
Add comment · Show 9 · 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 Rehtael · Dec 28, 2017 at 05:44 AM 0
Share

So when the left stick is centered (in its dead zone) the camera is facing forward with the ship. When the left stick is tilted, the camera turns with the stick, but is still relative to the ship.

To expand on my earlier example, let's say I've turned the ship to 180,90,0. I want the camera to also face 180,90,0 when the left stick is in its dead zone. If I push the left stick all the way to the left, I want the camera rotation to now be 180,0,0 and if I push it to the right the rotation would be 180,180,0.

avatar image Larry-Dietz Rehtael · Dec 28, 2017 at 06:11 AM 0
Share

Ok, I think I understand now. Sorry for the confusion.

At first glance, I would suggest taking out the + ship.rotation.x and + ship.rotation.y in your cockpit.transform.rotation code.

Since you have the cockpit as a child of the ship, it should already be matching the rotation of the ship, and as long as the cockpit is rotated to 0,0,0 it would be looking directly forward out the front of the ship, no mater what the ships rotation is. So your looking would be relative to the forward direction of the ship.

-Larry

avatar image Rehtael Larry-Dietz · Dec 28, 2017 at 06:24 AM 0
Share

Will give it a go, but I'm fairly sure I've tried that already. Still, worth a shot. I'll let you know what happens.

Show more comments
avatar image
0

Answer by Rehtael · Jan 02, 2018 at 04:41 AM

Been poking and prodding at this problem for a long while now, and I still can't figure out what is going wrong. Is there anyone out there with 2 cents?

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

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

447 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 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 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 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

Flip over an object (smooth transition) 3 Answers

How to make an object face the mouse in a non-2D world 1 Answer

RTS Camera movement wrong after rotation 1 Answer

move the object where camera look 0 Answers

how to clamp y axis with Quaternion.Euler in unity 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