I was wanting too have a system were I could build a tent, but since a tent doesnt have a foundation I cant just have that make up being on a hill, How would I go about solving this, or making a flat land below the tent ???
bump* anyone ?
You don’t build a tent on a hill in the first place. ![]()
terrain in a game is not always flat ,their would be exposed undertenet, and wouldnt touch ground like normal if it was placed ony type of normal terrain, I would have to have a completely flat, and boring terrain if I did it that way … I just need a way too flatten the terrain below me, and or a way too make sure the slope of the land I want too build on is smooth enough and if it is make the tent rotate too the slope of the land since it is smooth enough. I dont know how too do either XD
Make the part where the tent is on the terrain flat, like the top/bottom of a hill. And make hills around it… Ground isn’t always hilly.
Have you considered using rotation on your tent?
Yes but how would I make sure that the slope of the kill I am putting the tent on is not too great? and then figure out the rotation of the tent on that spot ?
you could make a wicker platform or something that the tent sits on with stilts coming from the bottom and make sure the bounds of the platform never passes through the ground while remaining level (which is how a tent should be ""level). This way the tent would be believably sitting on slopes as if they were built that way and wouldn’t look unnatural. Also when on flat ground the legs would not be seen under the platform and would look like a ground mat or something.
Edit: keep in mind that for stilts you still need to make a check to see if all the stilts make contact with the ground maybe a raycast from nodes placed at the leg to see if the ground is close enough that the stilts allways make contact to avoid floaty bugs that dont look nice. Using the raycast from each leg would make sure the ground is level too and could return a message of “Terrain too uneven” or something when any of the raycasts returns a null check.
Quick solution: Calculate the terrain height below your object, in this case, a tent, from at least three co-planar points. This will project a triangle on to the terrain and from there you can calculate slope and direction. At that point you simply rotate your object to match the slope and direction of the terrain. If the slope is too great, draw a visual indicator for the player or game designer (assuming they have the ability to place objects that indicates they cannot place the object there). You could also perform more samples of a finer granularity to make sure the tent is not being placed across a ravine, gully or ditch.
How about using a single ray and physics raycast and get the normal of the hit… this would give you a direction that “up” is on your tent. The rotation is then up to you.
That only works if the polygons making up your terrain are of the correct size in relation to the object being placed. Otherwise you may receive a polygon that is “flat enough” but in the area surrounding it is inappropriate. Or your object straddles two or more polygon edges of differing slopes.
I have also seen code where a bounding sphere or bounding box took an average on all of the polygons within the enclosed region and suitability was determined from that.
There are lots of methods for figuring this out, three points as a sample is the minimum to satisfy the appropriateness of the terrain test and still make the orientation look “good enough.” There will always be interpenetration issues, the only way around it is to have a super high resolution terrain (reality) and a tent that conforms to that terrain (reality). ![]()
OK, in testing my theory… I put a small demo together kind of display the concept.
http://lod3dx.net/Unity/Tent.html
I figured out that of all of the ways suggested, none actually work unless you make the terrain conform to the tent. This usually doesnt happen in games.
The advantage of what I propose is that your center is on the center of the hit. Other methods may not center the bottom but will average it along 3 points or more. I am not digging any of them for the most part.
OK, in the demo, I cast my first ray from the screen to the terrain. The second one is from a point above that hit, to the terrain, the last is from a point 1 unit in front of the tent, from the direction of the normal, in the inverted direction of the normal.
Yes, it was really that complicated. You then do two LookAt’s first, is for your rotation, the second is to get the forward rotation. The key here is to use the hit normal as the upward vector.
Instead of Layers I generally just get all hits , if I must just hits to a certain distance to save processor usage.
Here is the code that runs the tent in the demo.
function Start(){
SetHit();
}
function Update () {
if(Input.GetMouseButtonDown(0))
SetHit();
if(Input.GetKeyDown("r")) Application.LoadLevel(0);
}
function SetHit(){
var ray : Ray=Camera.main.ScreenPointToRay(Input.mousePosition);
var hit = GetRaycastHit(ray);
if(hit == null) return;
if(hit.point==Vector3.zero) return;
ray=new Ray(hit.point + Vector3.up * 2000, -Vector3.up);
hit = GetRaycastHit(ray);
if(hit == null) return;
transform.position=hit.point + hit.normal * (transform.localScale.y / 2);
transform.LookAt(transform.TransformPoint(Vector3.forward), hit.normal);
transform.localEulerAngles.y=Random.Range(0, 360);
ray=new Ray(transform.position + transform.forward + hit.normal * 2000, -hit.normal);
var lookHit = GetRaycastHit(ray);
if(lookHit == null) return;
transform.LookAt(lookHit.point + lookHit.normal * (transform.localScale.y / 2), hit.normal);
}
function GetRaycastHit(ray) : RaycastHit{
var nullHit : RaycastHit;
var hits = Physics.RaycastAll(ray);
if(hits.Length==0) return nullHit;
var hit : RaycastHit;
for(var i=0; i<hits.Length; i++){
hit=hits[i];
if(hit.collider.gameObject != gameObject){
break;
}
}
if(hit != null) return hit;
return nullHit;
}