• 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 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
Add comment · 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 $$anonymous$$ovementSpeed = 10;
 
 function update()
     {
       var xAxis= Input.GetAxis("Horizontal")*Time.deltaTime*$$anonymous$$ovementSpeed;
          transform.Translate(xAxis,0,0);
     }

(your X axis would correspond with horizontal movement assu$$anonymous$$g a top down view. Input.GetAxis will also return a value of -1 to 1, and assu$$anonymous$$g 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 program$$anonymous$$g.

I'm quite new to program$$anonymous$$g 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

· Add your reply
  • Sort: 
avatar image
1

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
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 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 setting up twin stick shooter for mobile device... 1 Answer

OnMouse menu 1 Answer

Speed pickup 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