I’ve been trying really hard to make some of my first code smarter. I’ve currently got 3 scripts that do exactly the same thing but in different directions. I’d like to make it so there’s one script where the direction is set by a public variable.
In this case direction1 is a public string variable.
The idea would be that it’d be used to set Vector3 to either forward/back/left/right/up/down so that the script can work dynamically with all directions.
transform.Translate(Vector3.(direction1));
I feel I’m probably going about this entirely the wrong way so hoping someone can help me out :).
Sounds like you are going in the right way but consider what Vector forward/backward/left etc actually represent. They are just shorthands, Vector3.forward is just (0,0,1), Vector3.back is (0,0,-1), Vector.down is (0,-1,0) etc
So In other words make direction a Vector3 instead of string.
Thanks for the reply, at first that appeared to be exactly what I needed to do but going further into my script I realised it doesn’t quite solve my problem.
The idea of the script is that there are public variables that set the axis the object will move along and the distance it will travel along that axis in each direction.
Having the direction set as a vector3 value works fine for the translation part of the script.
The issue comes from calculating how far along the axis it can travel.
My method so far is to have two public variables that set the upper and lower limit as a float. The idea of using a float is to make it independant from the axis of travel so that it can function no matter which direction the public variable says its to move in.
Because its the idea is that it will move along only one axis the script needs to know which axis its moving along, therefore I need some way of making the axis identifier a variable. (I think… :/)
Currently its followed later by this code which makes it move.
In simplest possible terms, I want the script to do what it does currently. However I want the axis of travel and the object’s maximum distance travelled in one direction to be variable.
Sorry if this isn’t clear, tell me if so and I’ll try to clarify. Thanks again for the help.
And then before that set myVector to depending on the variables.
if (!isForward)
myVector = Vector3.forward;
else if (isForward)
myVector = Vector3.back;
//we can add more directions like this
else if (isLeft)
myVector = Vector3.right;
//etc
As for the upper limit, simply use new parameters (you can make useForward, useLeft, and useUp) to set the different components of upperLimit:
if (useForward)
upperLimit.z = (transform.position.z + forwardLimit)
else if (useLeft)
upperLimit.x = (transform.position.x + forwardLimit);
//etc
The reason there’s such a tedious way of doing this is because there’s no “string to direction” function. You could try something with the Vector3 component index options… Maybe like
for (int i = 0; i < 3; i++)
{
upperLimit[i] = transform.position[i] + upperLimit;
}
And that would set upperLimit’s x, y, and z based off another position’s x, y, or z. But obviously this is where is gets complicated. And if you only want a single axis from upperLimit to be set, some more complicated stuff is necessary.
Thanks for the assistance guys, I think that’s exactly what I was looking for SomeGuy. I just couldn’t conceive the best method and what you’ve put there seems perfect. Thanks a bunch, I’ll see how I get on!