Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by Peysbubby · Jul 15, 2015 at 09:03 PM · c#camerarotationaxis

Is there a way to lock my camera's rotation and movement on certain axis?

I have my camera attached to an object in my scene and it is parented to it. Therefore my camera follows the object in all directions and rotations. however I don't want this. I want my camera to be locked on the y position axis, and the x, and z rotation axis. Using c#.

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
2
Best Answer Wiki

Answer by Piflik · Jul 16, 2015 at 12:07 PM

Parent an empty to the object, not the camera itself, and in Update set the camera to use that empty's x and z positions.

 [SerializeField] Transform target;
 
 void Update () {
     transform.position = new Vector3(target.position.x, transform.position.y, target.position.z); 
 }

The rotation is a bit more difficult, but you can calculate the necessary angle with a atan over the quotiont of the difference in the remaining axes.

e.g. if you want to only rotate along y, then the angle would be

 float angle = Mathf.Atan((target.position.x - transform.position.x)/(target.position.z - transform.position.z))      //Possibly wrong sign, depending on your setup

 transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, angle, transform.rotation.eulerAngles.z);


Alternatively, you can use a lookRotation to create a rotation to look at your object. If the position of the camera is fixed as above, this should only rotate along the remaing axis.

 transform.rotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up);
Comment
Add comment · Show 3 · 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 Peysbubby · Jul 16, 2015 at 04:06 PM 0
Share

hello, when I use this script:

 float angle = atan((target.position.x - transform.position.x)/(target.position.z - transform.position.z))      //Possibly wrong sign, depending on your setup
  
  transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, angle, transform.rotation.eulerAngles.z);

 

it tells me the name atan does not exist in the current context.

I cant seem to figure out why, would you please help me?

avatar image Piflik · Jul 16, 2015 at 04:35 PM 0
Share

Sorry, my bad. I started typing pseudocode before I noticed that it is nearly working C# and forgot to fix the first part. The function is part of the $$anonymous$$athf library.

 $$anonymous$$athf.Atan(...
avatar image Peysbubby · Jul 16, 2015 at 05:17 PM 0
Share

Thanks for all your help. However, this didn't quite give me the effect I was looking for in the rotation, but the

 [SerializeField] Transform target;
  
  void Update () {
      transform.position = new Vector3(target.position.x, transform.position.y, target.position.z); 
  }

worked perfectly for locking the position like I wanted.

I ended up using transform.LookAt(target);

and setting an empty game object towards the front of my player and setting that as target and this works.

thanks again for all of your help.

avatar image
2

Answer by KdRWaylander · Jul 16, 2015 at 11:33 AM

Hi,

Add a rigidbody to your cam, uncheck the 'Use Gravity' option unless what it will fall. Then, in 'Constraints', check all the axes (translation and/or rotation) you want to freeze :)

http://docs.unity3d.com/Manual/class-Rigidbody.html

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 DarkNode · Sep 08, 2016 at 01:13 AM 0
Share

Hey Waylander!

Thanks for that extremely simple and elegant solution. I was looking for something like this....

I like to see a solution that doesn't always involve additional scripting. :)

Cheers!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

move the object where camera look 0 Answers

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

RTS Camera movement wrong after rotation 1 Answer

Camera Rotation Question 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