• 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 NaZer · Oct 16, 2012 at 10:38 PM · cameragameobjecttriggeraxisalign

How do i align my camera with a gameobjects x and y axis?

(Im fairly new to Unity) Ok so i have these 2 rooms and i have the camera looking at the first room.

IDEA

I want it so that when the player enters the second room, the camera moves its view to the other room (in a 2D perspective)

alt text

PROBLEM

Ive tried using a box filling the room acting as a trigger, and OnTriggerEnter it would align the camera's x and y axis with the box trigger's axis. (But not the Z so it keeps the distance) but so far i have had no luck at all. and i have screwed around with the code so much now that i even find it irrelevant to post. Can anyone come up with a script that could do this, it would be MUCH appreiciated.

unity problem.jpg (93.4 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 Mrobertson · Oct 16, 2012 at 11:43 PM

I'd parent your camera to a game object (lets call it "CameraParent") and set the "CameraParents" position to the room1's position, then back your camera up to the distance and angle you want it to be. When your Player hits the trigger call a function to move the "Camera Parent" object to the next room

Possible Example:

 void OnTriggerEnter(Collider collider)
     {
         StartCoroutine("MoveCameraToNextRoom");    
     }
     IEnumerator MoveCameraToNextRoom()
     {
         float t = 1;
         while(t > 0)
         {
             cameraParent.position = Vector3.Lerp(oldRoom.position, newRoom.position, t);
             t -= Time.deltaTime;
             yield return null;
         }
     }

(not tested)

Comment
Add comment · Show 5 · 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 NaZer · Oct 17, 2012 at 12:55 AM 0
Share

I couldnt really get this to work, theres a lot of commands in there i dont know and its giving me alot of console errors. Such as:

(1,5): UCE0001: ';' expected. Insert a semicolon at the end.

(1,30): BCE0044: expecting ), found 'collider'.

(1,38): BCE0043: Unexpected token: ).

(3,46): BCE0044: expecting :, found ';'.

avatar image Mrobertson · Oct 17, 2012 at 01:06 AM 0
Share

from your errors it looks like your using javascript, I'm more comfortable with C# maybe you'll understand this better?

//next room trigger is hit

function OnTriggerEnter(other : Collider) {

StartCoroutine("$$anonymous$$oveCameraToNextRoom");
}

function $$anonymous$$oveCameraToNextRoom() {

var t = 1;

while(t > 0)

{

//set cameraParent, oldRoom and newRoom to something or you'll of course get a null exception

  cameraParent.position = Vector3.Lerp(oldRoom.position, newRoom.position, t);

  t -= Time.deltaTime;

//waits for one frame

  yield ;

} }

no clue how to get this to format correctly :(

avatar image NaZer · Oct 17, 2012 at 11:27 AM 0
Share

Whoops, yea i'm sorry i didn't clarify that it was Javascript :P

But so far my code looks like this:

//Variables

var cameraParent: GameObject;

var room1: GameObject;

var room2: GameObject;

//Assigning variables to gameobjects

function Start() {

 cameraParent = GameObject.Find("cameraParent");

 room1 = GameObject.Find("room1");

 room2 = GameObject.Find("room2");

}

//next room trigger is hit

function OnTriggerEnter(other: Collider) {

 StartCoroutine("$$anonymous$$oveCameraToNextRoom");

}

function $$anonymous$$oveCameraToNextRoom() {

 var t = 1;

 while (t > 0) {

     //set cameraParent, oldRoom and newRoom to something or you'll of course get a null exception

     cameraParent.position = Vector3.Lerp(room1.position, room2.position, t);

     t -= Time.deltaTime;

     //waits for one frame

     yield;
 }

}

And it detects the player colliding with the trigger but when it does it gives me a null reference that looks like this:

NullReferenceException: Object reference not set to an instance of an object triggerCameraScript+$$$anonymous$$oveCameraToNextRoom$5+$.$$anonymous$$oveNext () (at Assets/triggerCameraScript.js:25) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine(String) triggerCameraScript:OnTriggerEnter(Collider) (at Assets/triggerCameraScript.js:16)

I have tried using both GameObject.Find (seen above) and tried adding them manually in the editor but no difference.

avatar image Mrobertson · Oct 17, 2012 at 01:40 PM 0
Share

I had cameraParent room1, and room2 as Transforms, all you would have to do is either set your variables to Transform rather than gameobject or set cameraParent.position to cameraParent.transform.position

avatar image NaZer · Oct 17, 2012 at 03:32 PM 0
Share

Yea it works now, its just not very smooth it skips from on place to the other.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Aligning to a specific axis (6 DOF Descent-like movement) 1 Answer

How can I align Axis? 0 Answers

Camera to follow a target within a circle? 1 Answer

Gameobject to stay within the view of the camera - c# 3 Answers

Camera rotation the same as player rotation 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