- Home /
Restore player position using vector3 from variable.
Hello there. Hoping someone can help me.
I'm trying to restore the player position from a variable.
I'm trying to do it in JS as that's what I've got a hope of understanding.
The code I have so far is as follows:
// assume the variable 'restoreplayerpos' already contains the string "-17.6, 1.4, 21.2" without the quotes.
// restore player position
var playerObject = GameObject.Find("player");
playerObject.transform.position = Vector3(restoreplayerpos);
When I run the script I get an error:
"The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(String)'."
I need to convert the variable to what is expected as a Vector3 somehow, but don't know how. I've been googling this for ages. Please can someone help.
Answer by toddisarockstar · Feb 08, 2015 at 01:55 AM
var posx:float;
var posy:float;
var posz:float;
var words = new Array();
var realvector:Vector3;
words = stringyouarecoverting.Split(","[0]);
float.TryParse(words[0],posx);
float.TryParse(words[1],posy);
float.TryParse(words[2],posz);
realvector=Vector3(posx,posy,posz);
playerObject.transform.position = realvector;
if your string contains anything else like brackets this wont work you would have to use an extra line like this before you convert to eliminate whatever other characters in the string.
nameofstingtoconvert.Replace("(","");
nameofstingtoconvert.Replace(")","");
Thanks toddisarockstar. Now works a treat. $$anonymous$$y entire finished script looks like this if anyone is interested.
// to make a long story short, assume the variable myplayerloc already exists, and contains data. For example
var myplayerloc = "(-3.2, 12.0, 26.5)";
// ignore parenthesis
var restoreplayerpos : String = myplayerloc;
var foundS1: int = restoreplayerpos.IndexOf("(");
restoreplayerpos = restoreplayerpos.Substring(foundS1+1, restoreplayerpos.Length - 2 - foundS1);
// now format position of player so we can restore the gameobject
var sStrings = restoreplayerpos.Split(","[0]);
var myposx : float = float.Parse(sStrings[0]);
var myposy : float = float.Parse(sStrings[1]);
var myposz : float = float.Parse(sStrings[2]);
// find the player object and set it's location
var playerObject = gameObject.Find("player");
playerObject.transform.position = new Vector3(myposx, myposy, myposz);
Answer by AngryBurritoCoder · Feb 07, 2015 at 09:20 PM
You cannot put a string into a Vector3, basically instead of assigning a string assign a Vector3 variable doing it like this ( same as making a int or string no difference )
public Vector3 playerResetPos = new Vector3(-17.6f, 1.4f, 21.2f); ( cant remember if there is new or not in front, my memory dissapears sometimes) now you can use this...
playerObject.transform.position = playerResetPos;
Answer by tperry1x · Feb 07, 2015 at 09:36 PM
Wow, that was quick. Thanks!
Did what was suggested, but now I'm getting the three following errors in unity:
expecting }, found 'public'.
';' expected. Insert a semicolon at the end.
expecting EOF, found '}'
So, just to explain. My script works like this. It'll read a file in the subfolder 'data' and looks for 'mydata.txt'. It'll then read the second line of that data, which currently contains:
(-7.4, 1.4, 6.5)
It'll then remove the brackets. Then it stores -7.4, 1.4, 6.5 in the restoreplayerpos variable Everything fails when I try to restore the player position based on this variable. I'll get this error:
'The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(String)'.
I assume this is because Unity isn't formatting the variable somehow?
Strangely enough, I can type
playerObject.transform.position = Vector3(-7.4, 1.4, 6.5);
and that'll work absolutely fine, even though the variable means the same thing!
it doesnt mean the same thing because you need to convert the strings into floats and put into vector3 if you get what i mean, because atm it is same as saying = Vector3(text,text,text);
Ok, that makes sense. So, silly question as I'm new at all this, but how do I simply convert the restoreplayerpos variable containing -7.4, 1.4, 6.5 to floats as needed?
print(restoreplayerpos);
// variable contains -7.4, 1.4, 6.5
// restore player position
var playerObject = GameObject.Find("player");
playerObject.transform.position = Vector3(restoreplayerpos); // need to convert the variable restoreplayerpos into a float somehow?
Answer by toddisarockstar · Feb 08, 2015 at 01:55 AM
that last answer i believe was in C# which is giving you additional errors when its in your javascript.
the whole problem is that a vector3 variable contains a set of three seperate NUMBERS. restoreplayerpos is a string variable. so the the computer sees it as just one variable of text characters that dont nessisarily have a mathmatical value. So your problem is more complex than you think! I can try to get you in the right direction. it is possible to convert your string to the three numeric values to form a Vector3.
if your restoreplayerpos is formated exactly like this -17.6, 1.4, 21.2 this code will split your string at the commas and push the three variables into an array called words.
var words = new Array();
words = restoreplayerpos.Split(","[0]);
now you have 3 seperate strings for your vector3 and eliminated the commas characters so we can convert them out of string format.
var posx:float;
var posy:float;
var posz:float;
float.TryParse(words[0],posx);
float.TryParse(words[1],posy);
float.TryParse(words[2],posz);
now we got 3 actual numbers with a value out of the string so lets put them together to make a real Vector3.
var realvector:Vector3;
realvector=Vector3(posx,posy,posz);
playerObject.transform.position = realvector;
just paste these lines in your code to declair your position. it will work. read up on strings!
var posx:float;
var posy:float;
var posz:float;
var words = new Array();
var realvector:Vector3;
words = stringyouarecoverting.Split(","[0]);
float.TryParse(words[0],posx);
float.TryParse(words[1],posy);
float.TryParse(words[2],posz);
realvector=Vector3(posx,posy,posz);
playerObject.transform.position = realvector;
and btw,if your string contains anything else like brackets this wont work you would have to use an extra line like this before you convert to eliminate whatever other characters in the string.
nameofstingtoconvert.Replace("(","");
nameofstingtoconvert.Replace(")","");
Your answer
Follow this Question
Related Questions
Slerp to make the right/left side face another object 2 Answers
Is it posible to attach a crosshair to an image in vuforia? 0 Answers
assigning vector 3 through scene positioning 0 Answers
How can I assign an Array of Vector3 while seeing and editing the values in the Scene? 0 Answers
FPS Controller Confusion... 0 Answers