- Home /
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>waterLevel+15)
{
pos=transform.position;
}
}
edited to correct sloppy code
edited to reflect new code accomplishing task(though not the intended way)
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.
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.
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.
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.
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

Follow this Question
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