Let's add a terrain GameObject-3D-Terrain.
Import Characters package Assets-Import Package-Characters, select all, and click Import.
Find the ThirdPersonController in the Project pane(use the search tool if you can't find it) and add it to the scene.
Create a Cube GameObject-3d Object-Cube. Name it ShootBox.
Create a Sphere GameObject-3d Object-Sphere, set its scale to 0.5 for x,y,and z. Name it BulletPlaceHolder.
Create a folder Resources.
Drag the two recently created objects into the Resources folder.
Create a Scripts folder.
Create three scripts: Bullet.cs, Player.cs,ShooterBox.cs, and add their respective code
Bullet.cs
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour
{
private float Speed = 3.5f;
public GameObject Target = null;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Target != null)
{
float step = Random.Range(1,5) * Time.deltaTime;
this.transform.position = Vector3.MoveTowards(this.transform.position, Target.transform.position + new Vector3(0,0.5f,0), step);
}
}
public void OnTriggerEnter(Collider other)
{
Debug.Log(other.name);
Player playerComponent = other.GetComponent<Player>();
if (playerComponent != null)
{
int newHealth = playerComponent.Health - 1;
if (newHealth == 0)
{
Time.timeScale = 0;
}
else
playerComponent.Health = newHealth;
GameObject.Destroy(this.gameObject);
}
}
}
Player.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Player : MonoBehaviour {
public int Health = 100;
public Text HealthText;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
HealthText.text = Health.ToString();
}
}
ShooterBox.cs
using UnityEngine;
using System.Collections;
public class ShooterBox : MonoBehaviour {
// Use this for initialization
private GameObject player;
private float lastTimeShooted = 0;
public int ShootIntervalInSeconds = 15;
void Start () {
this.player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update () {
if ((lastTimeShooted + ShootIntervalInSeconds) < Time.time)
{
GameObject loadedResource = Resources.Load<GameObject>("BulletPlaceHolder");
GameObject newInstance = GameObject.Instantiate<GameObject>(loadedResource);
newInstance.transform.position = this.transform.position;
Bullet bullet = newInstance.GetComponent<Bullet>();
bullet.Target = this.player;
lastTimeShooted = Time.time;
}
}
}
Add the Bullet.cs script to the BulletPlaceHolder prefab.
Add the ShooterBox.cs script to the ShooterBox prefab.
Add the Player.cs script to the ThirdPersonController in the scene
Create a Canvas object and add two objects to it: HealthLabel, HealthText
Go to the ThirdPersonController in the scene, in the inspector find the Health Text field below the Player script and assign the recently created HealthText object.
No hay comentarios.:
Publicar un comentario
Nota: sólo los miembros de este blog pueden publicar comentarios.