• 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
Question by Laghari · Jun 07, 2012 at 10:48 AM · inputtext file

How to move my character by getting input from text file?

i want to move my character in the game from input through a text file. On 1 i want to move it right..On 0-Idle and on -1 towards left..i am using the following code:

 var fileContents = sr.ReadToEnd();
 sr.Close();
 
 var lines = fileContents.Split("\n"[0]);
 for (line in lines) {
     
     h = TryParseInt(line);

This code moves the character to left when i ONLY give -1 in the txt file and to right when i give only 1 and remains idle when i give ONLY 0....bt when i give inpput "1,0,-1" together it does not give the desired result of moving first "right,idle,left".

Please help me with this as i am new to programming so i dnt know alot and hence asking this question.

Thankyou.

Comment

People who like this

0 Show 2
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 RsVellinga · Jun 07, 2012 at 12:24 PM 0
Share

may I ask why you would want to do this? If this is for your charracters movement why not use:

 var MovementSpeed = 10;
 
 function update()
     {
       var xAxis= Input.GetAxis("Horizontal")*Time.deltaTime*MovementSpeed;
          transform.Translate(xAxis,0,0);
     }

(your X axis would correspond with horizontal movement assuming a top down view. Input.GetAxis will also return a value of -1 to 1, and assuming input is with keyboard it will always return -1, 0 or 1. essentially doing exectly what your goal is.

If this is for say enemy behaviours (for example for a scrolling shooter) wouldnt it be easier to just hard code each enemies behaviour in a seperate script? especially if your new to programming.

I'm quite new to programming myself (started on making my first game, a top down scrolling shooter about 2 weeks ago) so i wonde why you would wand something like that while just learning to program

avatar image Laghari · Jun 07, 2012 at 03:34 PM 0
Share

@RsVellinga i am controlling the avatar through brain...so i m storing those brain signals after working on them in a file...and taking it as an input in the game....and jst got stucked up on this last step...have completed the rest difficult part.... :(

1 Reply

  • Sort: 
avatar image

Answer by TheBetpet · Jun 07, 2012 at 12:51 PM

I've not tried the code myself but it seems to me that if you're storing your moves as 1,0,1,1,0 that when you try and parse that line, you're trying to parse the whole thing instead of the numbers from it. Try:

var moves = line.Split(",");

then iterate through each of the moves in the 'moves' variable. i.e.

for (line in lines) {

 var moves = line.Split(",");

 foreach (string move in moves) {

 h = TryParseInt(move);
Comment
whydoidoit

People who like this

1 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 aldonaletto · Jun 07, 2012 at 01:21 PM 0
Share

I converted your comment to answer - I think it can solve the OP's problem (except by the foreach - it's just for in Unityscript)

avatar image Laghari · Jun 07, 2012 at 03:39 PM 0
Share

var sr = new StreamReader(Application.dataPath + "/" + control_fileName);

 var fileContents = sr.ReadToEnd();
 
 sr.Close();

 for (line in lines) {

 var moves = line.Split(",");

 foreach (string move in moves) {

 h = TryParseInt(move);

I have tried bt it is giving error... " Error BCE0044: expecting ), found 'move'. " It is expecting ) after string.... :s Please help wid dis silly question..

avatar image TheBetpet · Jun 07, 2012 at 04:15 PM 0
Share

aldonaletto converted my answer (my bad). It's for not foreach

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Write to a file 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Problem in player movement using accelerometer input 0 Answers

Input.GetAxis returns 0 after scene load 1 Answer

KeywordRecognizer does not seem to work 0 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