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 mephisto8 · Dec 30, 2013 at 11:30 PM · camerarotationorientation

Simple camera question -- follow rotation of object

How can I get my camera to follow this object's rotation (about y) as well as position?

alt text

alt text

The object is three cylinders (two wheels and an axle) connected by joints. All three cylinders are children of an empty parent Gameobject called 'Segway'. Intuitively, the orientation of the object is the orientation of the axle (ignoring the fact that it's spinning).

The problem with making the camera a child of Segway is that Segway's rotation doesn't change when the compound object it contains rolls around. The problem with making the camera a child of one of the cylinders (wheel or axle) is that the cylinders rotate like crazy.

5seo6.gif (379.8 kB)
screen shot 2013-12-30 at 22.51.45.png (41.3 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Dec 31, 2013 at 01:37 AM

I'm going to assume that the axle is constructed from a cylinder and therefore the Transform.up of the axle will be along the axis of the cylinder. With that assertion, here is a bit of code that will cause the camera to follow and rotate to the axle:

 #pragma strict
 
 public var axle : Transform;
 public var camHeight = 2.0;
 public var camDist = 4.0;
  
 function LateUpdate() {
     var v = Vector3.Cross(axle.up, Vector3.up) * camDist;
     v.y += camHeight;
     transform.position = axle.position + v;
     transform.LookAt(axel);
 }

'camDist' is the distance behind the axle to follow. 'camHeight' is the height to place the camera above the axle. You will need to initialize 'axle' either by dragging and dropping in the Inspector, or you can use something like GameObject.Find() or GameObject.FindWithTag() in the Start() to initialize the variable. I may have the two parameters in the Vector3.Cross() reversed.

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 mephisto8 · Dec 31, 2013 at 02:53 PM 0
Share

Thanks very much for this. I tried this on a new camera in the scene and it's a bit closer to what I'm after, but not quite. I don't want the camera to actually look down at the axel, but just to copy the axle's rotation around y. So when camHeight and camDist are very small, I want the camera to be looking at the horizon and not down at the ground. $$anonymous$$y fault for not being clearer.

I was trying to keep my question above as simple as possible, but I should say a little more about what I'm up to be clearer. $$anonymous$$y object is a very basic Segway-type vehicle that I want to use for moving around a scene. You can see from the tiny animated gif in my original post how it moves. I am using a prefab camera with its own controller script from Oculus VR. This camera is set up to track the rotation of a head-mounted display (allowing the user to look around the scene). Since I don't want to interfere with how this prefab camera works, I'm sticking it inside of an empty Gameobject called 'CameraEmpty', and I'm controlling it with the following script:

 using System.Collections;
 
 public class CameraEmptyController : $$anonymous$$onoBehaviour {
 public GameObject segway;
 private Vector3 offset;
 
 void Start () {
     offset = transform.position;
     }
     
 void LateUpdate () {
     transform.position = segway.transform.position + offset;
     }
 }

That's what I have so far. The camera moves around the scene with the segway, but is in a fixed orientation. I've tried rather futilely to find the right bit of code to read off the y rotation from the axle and set it as the y rotation of the CameraEmpty (the empty object containing the special Oculus VR camera). The attempts below (within the controller script attached to CameraEmpty) obviously failed, but you can see what I'm getting at:

 //  transform.rotation.y = segway.transform.rotation.y;
 //    transform.eulerAngles = Vector3(0, segway.eulerAngles.y, 0);    
 //    transform.rotation = Quaternion.Euler(segway.transform.rotation.y);
 

 
avatar image robertbu · Dec 31, 2013 at 04:30 PM 0
Share

Try this and let me know how it works for you:

 #pragma strict
  
 public var axle : Transform;
 public var camHeight = 2.0;
 public var camDist = 4.0;
  
 function LateUpdate() {
     var v = Vector3.Cross(axle.up, Vector3.up) * camDist;
     var lookDir = v;
     v.y += camHeight;
     transform.position = axle.position + v;
     transform.rotation = Quaternion.LookRotation(lookdir);
 }

Note I could help you calculate the rotation for your existing code, but I don't think that is what you need. If the 'segway' object is not rotating, then the position will be wrong.

avatar image mephisto8 · Dec 31, 2013 at 05:53 PM 0
Share

Robert, you're a gentleman and a scholar. This is exactly what I wanted. Works straight off with a standard camera within my empty Gameobject (to which I've attached your script). The OVR camera was a little trickier (it still had fixed orientation), but its own controlling script comes with a 'Follow Orientation' public variable, to which I've assigned the empty Gamobject containing it (which in turn is controlled by your script).

As soon as I get voting rights I'll make sure I vote up your answer. $$anonymous$$any thanks.

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

18 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

Related Questions

[Solved :D] Camera auto turns 180° from its init position on Play (Animated GIF Included!) 1 Answer

Rotate an object around another on input 0 Answers

Rotation seems to pass through condition checks 1 Answer

I can not set a orientation in a build setting. 2 Answers

How to make my player look in the direction of its movement? 3 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