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 digitalConundrum · Dec 29, 2010 at 05:05 AM · javascriptlogic

Basic beginning javascript question. (logging position and moving character to logged position)

Hi..this is one of my first forays into javascript (nearly all of my experience is in c++), and I've hit a snag that I cant quite find on my own...this is a (very) simple script meant to log the player's position every 5 seconds and to relocate the player to the logged position if they enter water. It is executing correctly except that the position is not being logged; only the initial position is being used.

Also, when the code is compiled inside unity, the variable waterLevel does not show up as being alterable without actually opening the script and changing the code directly. Is there a trick to making it do so?

I apologize for asking such a basic question on here, but I'm not sure what exactly the problem is...It compiles fine, but the behavior I think I wrote just isnt happening.

UPDATE: Although I could not get the 5-second interval to work correctly, I did manage to get it to log the position every time the script is run as long as the controller is 15 units above the set sea level.

Thanks for the answers; I appreciate them.
Have a nice day.

private var pos : Vector3; var waterLevel :int = 90;

function Start () {

 pos=transform.position;
 counter=0;

}

function Update () {

if (transform.position.y<waterLevel) { transform.position=pos; counter=0;

         }




     if (transform.position.y&gt;waterLevel+15)
         {
         pos=transform.position;     
         }

}


edited to correct sloppy code

edited to reflect new code accomplishing task(though not the intended way)

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

2 Replies

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

Answer by Eric5h5 · Dec 29, 2010 at 05:19 AM

There are several problems here...the variables aren't typed as anything (except for "waterLevel"), which is probably the main issue, the code you posted isn't valid (copy & paste error?), "1*Time.deltaTime" is redundant, instead of using 3 separate variables for the position use a single Vector3, and it would be simpler to use InvokeRepeating instead of Update.

Comment
Add comment · Show 2 · 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 digitalConundrum · Dec 29, 2010 at 05:23 AM 0
Share

Alright...I thought I read something about implicit casting in js, but ill try explicitly casting them; can't hurt. Fair enough on the "1*Time.DeltaTime" I'm honestly a bit embarrassed about that one. And you just showed me why my initial Vector3 implementation attempt didn't work; capitalization error. Thank you very much, Ill try those tips out now. I appreciate the answer.

avatar image Eric5h5 · Dec 29, 2010 at 05:49 AM 0
Share

If you're referring to type inference, you need to give something to the compiler so it can figure the type out. "var waterLevel = 90;" works, because you supplied an integer, so the compiler figures that waterLevel should be an int. In this case, "var waterLevel : int = 90;" doesn't do anything extra. However, "var waterLevel;" could cause problems (as well as slowness) because it's not typed as anything...the type isn't supplied, and the compiler can't figure it out because there's nothing to go on.

avatar image
0

Answer by ina · Dec 29, 2010 at 05:10 AM

Did you drag and drop this into a scene GameObject? The global variables should be exposed in the inspector of that object, once you do so.

Comment
Add comment · Show 1 · 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 digitalConundrum · Dec 29, 2010 at 05:20 AM 0
Share

yes, the script is attached to the fps controller game object. And now that I reloaded the scene the variable does indeed show up as being editable. The position logging is still not working however. It still places the controller back at the starting position and not a position taken at 5 second intervals.

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

No one has followed this question yet.

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Healt Bar Logic 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Object instantiation Logic Javascript. 1 Answer

Need help with logic problem (JavaScript) 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