- Home /
Spawning System
I have a empty game object called "SpawnPoint". Obviously you probably know what im going to ask. I want people once there health = 0 to teleport here. Here is my script at this moment.
using UnityEngine; using System.Collections;
public class JobInfo : MonoBehaviour { public int curMoney = 1000; public int curHealth = 100; public int maxHealth = 100; public string curJob; public int Salary = 50; public int payTime = 180;
public float healthBarLength;
void Start () {
InvokeRepeating ("AddMoney", payTime, payTime);
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AdjustCurrentHealth(0);
}
void OnGUI() {
GUI.Label (new Rect (10, 10, 100, 30), "Job: ");
GUI.Label (new Rect (35, 10, 100, 30), curJob);
GUI.Label (new Rect (85, 10, 100, 30), "Salary: ");
GUI.Label (new Rect (125, 10, 100, 30), Salary.ToString());
GUI.Label (new Rect (155, 10, 100, 30), "Money: ");
GUI.Label (new Rect (200, 10, 100, 30), curMoney.ToString());
GUI.Box(new Rect(10, Screen.height - 30, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AdjustCurrentHealth(int adj) {
curHealth +=adj;
if(curHealth < 1)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
void AddMoney ()
{
curMoney += Salary;
}
}
Im guessing that it should start out like this...
if(curHealth = 0)
than something here.
I know ill need a variable, most likely or just to make stuff easier so would the variable look something like this?
public GameObject PlayerSpawn;
or would the variable have anything to do with Vector3? Im just looking for help and want to understand this more.
The SpawPoint is located at
X: 1613.222
Y: 1
Z: 1548.858
Answer by PrimeDerektive · Jan 05, 2011 at 03:59 AM
First off, try to use camelCase for vars and properties, and reserve CamelCase for functions and methods, its good practice. Then, make the variable
public Transform playerSpawn;
Just so you have access to the transform component right off the bat, and drag the object from your scene hierarchy to the exposed playerSpawn var in the inspector. Then, in your if statement, do:
if(curHealth == 0)
transform.position = playerSpawn.position;
Note: I don't normally use C#, but I think my syntax is right.
great and thanks for explaining it to me it really helps... and if you see see comment can u reply and tell me an example of when you would use a Vector3?
well, transform.position IS a Vector3, its just a shortcut var of the transform component that tells you the current Vector3 position of that object in the scene.
Your answer
Follow this Question
Related Questions
Navmesh agent teleporting 3 Answers
Enemy spawn script/player position(java/js) 1 Answer
can I change float to vector3 on euler ?? 1 Answer
Snapping to Object Forward Axis 0 Answers
Player not spawning in right place. 1 Answer