- Home /
Duplicate Question
Vector3 is a 'type' but it is used like an 'variable' (C#)
Vector3 MapMaxBounds;
Vector3 MapMinBounds;
var MapTransform = Map.transform;
MapMinBounds = Vector3(MapTransform.position.x, 0, MapTransform.position.z);
MapMaxBounds += MapMinBounds+Vector3(Map.terrainData.size.x, 0, Map.terrainData.size.z);
I don't know what I did wrong.
Answer by Bunnybomb7670 · May 22, 2014 at 10:01 PM
you need to make a NEW Vector3. That code tries to reference the main class when you really want to make an instance of it. so, instead you want to do :
new Vector3(MapTransform.position.x, 0, MapTransform.position.z);
http://www.dotnetperls.com/new may help a bit to explain the reason why.
Thank you now it nearly works :) but I have another problem: I tried this solution of map-borders from another thread but I got the error "Cannot modify the return value of 'UnityEngine.Transform.position' because it is not a variable" but the guy who wrote it said it works
Here is the code: the error is in void update (transform.position.x = $$anonymous$$ap$$anonymous$$axBounds.x)
public Vector3 $$anonymous$$ap$$anonymous$$axBounds;
public Vector3 $$anonymous$$ap$$anonymous$$inBounds;
public Terrain $$anonymous$$ap;
void Start ()
{
float bounds = 0.00f;
var $$anonymous$$apTransform = $$anonymous$$ap.transform;
$$anonymous$$ap$$anonymous$$inBounds = new Vector3($$anonymous$$apTransform.position.x, 0, $$anonymous$$apTransform.position.z);
$$anonymous$$ap$$anonymous$$axBounds = $$anonymous$$ap$$anonymous$$inBounds + new Vector3($$anonymous$$ap.terrainData.size.x, 0, $$anonymous$$ap.terrainData.size.z);
$$anonymous$$ap$$anonymous$$inBounds.x += bounds;
$$anonymous$$ap$$anonymous$$inBounds.z += bounds;
$$anonymous$$ap$$anonymous$$axBounds.x -= bounds;
$$anonymous$$ap$$anonymous$$axBounds.z -= bounds;
}
void Update ()
{
RTSCam$$anonymous$$ove ();
RTSCamRotate ();
if(transform.position.x > $$anonymous$$ap$$anonymous$$axBounds.x) {
transform.position.x = $$anonymous$$ap$$anonymous$$axBounds.x;
}
if(transform.position.z > $$anonymous$$ap$$anonymous$$axBounds.z) {
transform.position.z = $$anonymous$$ap$$anonymous$$axBounds.z;
}
if(transform.position.x < $$anonymous$$ap$$anonymous$$inBounds.x) {
transform.position.x = $$anonymous$$ap$$anonymous$$inBounds.x;
}
if(transform.position.z < $$anonymous$$ap$$anonymous$$inBounds.z) {
transform.position.z = $$anonymous$$ap$$anonymous$$inBounds.z;
}
}
Basically, you cannot directly modify the values of a Struct, Vector3 is a struct, so ins$$anonymous$$d you would need to do something like :
if (transform.position.x > $$anonymous$$ap$$anonymous$$axBounds.x)
{
transform.position = new Vector3($$anonymous$$ap$$anonymous$$axBounds.x, transform.position.y, transform.position.z);
}
You basically need to make your own new instance of a Vector3, set its values, then set the transforms position to your new struct. Like I just demonstrated above.
Follow this Question
Related Questions
Is it possible to use a variable as a Type? 1 Answer
Multiple Cars not working 1 Answer
How can i change a childs local position with a variable? 1 Answer
How to declare the type after a string? 1 Answer
Distribute terrain in zones 3 Answers