Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Importing coordinates from a external file and using them to reposition the camera

Discussion in 'Getting Started' started by Whitt1985, Aug 12, 2015.

  1. Whitt1985

    Whitt1985

    Joined:
    Aug 2, 2015
    Posts:
    10
    Hi Everyone,

    While I'm a good programmer, I'm still getting used to the link between scripting and the objects within the level.
    Simply put I am trying to move the camera to coordinates taken from a external ascii file. Where do I start with this? I know I have to make a script within the main camera but I am unsure how to reference the camera position from within this script. In addition to this I am unsure how to import a ascii file that will update constantly at run time. I am working on the Android platform.

    Many Thanks

    James
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    What kind of programming do you normally do? I'm not sure that I'd be messing with ASCII files if there was a need for it to be constantly updated... The amount of resources you'll spend reading and writing to that file repeatedly suggests there's gotta be a better way. Or is a remote text file your only option?

    What are you actually trying to do here? As in, big picture.
     
  3. Whitt1985

    Whitt1985

    Joined:
    Aug 2, 2015
    Posts:
    10
    Thanks for the reply,

    Best described I do a lot of academic programming, the fortrans and c of this world, haven't done object orientated for a while! Recently picked up Android Studio and getting on with that nicely though.

    So big picture for this is a simple AR tech test. I've got a app which does indoor positioning and outputs the position to a ascii file on the phone. The plan is to have that running in the background, output my position in the room. Next Unity will take in this information and reposition the camera in scene according to that position. I don't need to write to the file at all.

    Next step is I intend to use the google cardboard demo scene which nicely allows the use of the camera sensors for pitch and yaw of the phone combining this with positioning changing according my IPS app means I should be able to walk around my scene using the phone. The final step would be turn on the phone camera through Unity thereby creating a AR effect without using image positioning. That's the hope anyway!

    If you have any examples which could do the above or code snippets it would most certainly help me along the way!

    James
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    OK, given what you're trying to do here I think this approach is reasonable.

    Your script on the camera object can access the camera position as simply "transform.position". You can open and read a file using standard C# file I/O. So, periodically read the file, parse out the position it contains, and assign it to transform.position, and Bob's your uncle.
     
    Schneider21 likes this.
  5. Whitt1985

    Whitt1985

    Joined:
    Aug 2, 2015
    Posts:
    10
    Hi Guys,
    Got it working last night, took a bit of tinkering with the path, but was all good in the end!

    Thanks for the help
    James
     
    JoeStrout likes this.