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 LordJulian · Oct 25, 2011 at 05:38 AM · camerahelicopter

Aligning Camera to Object

I've used the MouseOrbit Script from the Assets and placed it on my helicopter in order to create a cockpit view that the player can rotate the camera around with the mouse. I altered abit and it works smoothly, however when I try to rotate my helicopter, the camera doesnt align itself to rotate with the helicopter. I've placed the script on the camera that is linked to the helicopter object

This is the modified MouseOrbit Script:

 var target : Transform;

var distance = 10.0;

var xSpeed = 250.0;

var ySpeed = 120.0;

var yMinLimit = -20;

var yMaxLimit = 80;

var xMinLimit = -90;

var xMaxLimit = 90;

private var x = 0.0;

private var y = 0.0;

@script AddComponentMenu("Camera-Control/Mouse Orbit")

function Start () {

 var angles = transform.eulerAngles;

 x = angles.y;

 y = angles.x;



 // Make the rigid body not change rotation

    if (rigidbody)

     rigidbody.freezeRotation = true;

}

function LateUpdate () {

 if (target) {

     x += Input.GetAxis("Mouse X") * xSpeed * 0.02;

     y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

      

      y = ClampAngle(y, yMinLimit, yMaxLimit);

      x = ClampAngle(x, xMinLimit, xMaxLimit);

             

     var rotation = Quaternion.Euler(y, x, 0);

     var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;

     

     transform.rotation = rotation;

     transform.position = position;

 }

}

static function ClampAngle (angle : float, min : float, max : float) {

 if (angle < -360)

     angle += 360;

 if (angle > 360)

     angle -= 360;

 return Mathf.Clamp (angle, min, max);

}

How do I fix this to allow my camera view to link with the helicopter whenever it rotates whilst it rotates the view around?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by roamcel · Oct 25, 2011 at 07:04 AM

Your question is related to a basic parenting issue, which is not so easily resolved in code.

Basically, just drop your camera as a child of the helicopter object in your game scene. You'll immediately see that when you rotate the helicopter, the camera will rotate alongside it... fighting with your mouse input.

Usually, what you need to do in such cases is adapt your mouse input script to 'ease' the camera rotation when you're inside the helicopter, applying interpolation or dampening of the input values.

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 LordJulian · Oct 27, 2011 at 07:59 AM 0
Share

Actually, the rotation of the helicopter isn't via the mouse input, but by 'A' and 'D' buttons.

avatar image
0

Answer by kampma · Oct 29, 2011 at 11:22 PM

I assume you had the same issues I was having. I wanted to be able to keep my "mouseorbit" 3rd person camera aligned to the local axis of an aircraft, however my X and Y inputs were not being reoriented to the aircraft axes during flight. Instead, pressing up on my joystick's hat switch would lead to a camera change in the up-direction in the world axis. This occurred whenever my aircraft was not wings level. Same issue for adjusting the yaw orientation about the aircraft. I have an aircraft as the parent and my camera "childed" to it. The original MouseOrbit script included in unity uses Euler angles, but I made a few changes for it to use Quaternions for the transform rotations. Hope this helps!


var target : Transform; var distance = 50.0; var height = 5;

var lookSpeed = 250.0; var moveSpeed = 50.0;

private var rotationX = 0.0; private var rotationY = 0.0;

@script AddComponentMenu("Camera-Control/Camera Orbit")

function Start () {
var angles = transform.localEulerAngles; // target WORLD SPACE rotationX = angles.x; // Eulerangles.x = rotation z degrees about z axis target WORLD SPACE rotationY = angles.y; // Eulerangles.y = rotation x degrees about x axis target WORLD SPACE rotationZ = angles.z; // Eulerangles.z = rotation y degrees about y axis target WORLD SPACE

 // Make the rigid body not change rotation
    if (rigidbody)
     rigidbody.freezeRotation = true;

}

function LateUpdate () { if (target) {

       if (Mathf.Abs(Input.GetAxis("LookPitchAxis")) > 0.05) 
         {rotationY -= Input.GetAxis("LookPitchAxis") * lookSpeed * 0.02;}  

     if (Mathf.Abs(Input.GetAxis("LookYawAxis")) > 0.05) 
         {rotationX += Input.GetAxis("LookYawAxis") * lookSpeed * 0.02;}    
                                                                                         
     // Reset view button action  (directly behind aircraft)                                                                                                                                                                                                                                
      if (Input.GetButtonDown ("ResetView")) {
         rotationX = target.transform.eulerAngles.x; //  rotation.x   // target WORLD SPACE   target.transform.ect
         rotationY = target.transform.eulerAngles.y; //  rotation.y   // target WORLD SPACE
         rotationZ = target.transform.eulerAngles.z; //  rotation.z   // target WORLD SPACE
     }
     
                                                                                   
     transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
     transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
     transform.position = transform.rotation * Vector3(0.0, 0.0, -distance) + target.position;
         
 }

}


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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

changing the target of camera through script 1 Answer

Camera Movement and angles 2 Answers

Is it possible to shake the camera? 4 Answers

Make Arms Move Slow 0 Answers

Toggle between scripts with a script? 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