Alright, I’ve been developing a RTS game and I’ve run into these problems.
Please understand that I’m a noob still with Unity and this project is just to get my skills better so try to explain things as good as you can and try to provide scripts for me to learn. I think I’ve tried everything already what I can do with Unity.
-
How to get buildings on top of the terrain? And how to check that terrain under that building is buildable (flat, not in a water or something else.) Now they spawn half way inside a terrain.
-
Is it possible to do so that when you do collision with trees with tanks they do fall down? How?
-
How to stop camera going outside of level? So that it will stop on border of the current level?
Thank you in advance.
This is my building script:
// Buildings health here.
var buildingHealth : float = 100;
var buildingTypes : GameObject[];
private var buildIndex : int = 0;
// Add a healthbar for buildings.
var healthBarPrefab : GameObject;
private var healthBar : GameObject;
var resourceCash : int = 100;
// Lets check if you click building.
function Start() {
var go : GameObject = GameObject.Find("UnitManager"); // Finding right file to include.
go.SendMessage("AddUnit", gameObject);
// Lets tell unity where Healthbar should be.
healthBar = Instantiate(healthBarPrefab, transform.position, Quaternion.identity);
healthBar.transform.parent = gameObject.transform;
healthBar.transform.position.y += 5;
// Set unit selected as false.
SetUnitSelected(false);
}
// This handles a collision, what happens when you shoot it.
function OnCollisionEnter (collision : Collision) {
buildingHealth -= 22;
healthBar.GetComponent("HealthBar").transform.localScale.x = buildingHealth/100.0;
if (buildingHealth < 1)
{
Destroy(this.gameObject);
}
}
function SetUnitSelected(selected : boolean) {
isSelected = selected;
healthBar.GetComponent("HealthBar").SetHealthEnabled(isSelected);
}
function SetSelected() {
print("I got selected... " + name);
var go : GameObject = GameObject.Find("UnitManager");
go.SendMessage("AddSelectedUnit", gameObject);
}
function OnGUI(){
if(GUI.Button(Rect(0,180,100,40),"Guard Tower")){
buildIndex = 1;
}
else
{
buildIndex = 0;
}
}
function Build(buildingTypes : int){
if(buildingTypes==1){ // removing resources
if(resourceCash>50){
resourceCash-=50;
}
}
}
function Update(){
if(Input.GetMouseButtonDown(2)){
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 300)) {
Instantiate(buildingTypes[buildIndex],hit.point,Quaternion.identity);
}
}
}