Menu

MatrixAR客户端代码设计

2018年7月6日 - 游戏开发

这款AR游戏采用了高通的Vuforia引擎,开发

分为客户端,和局域网对战,局域网是Networking做的开发,使用的状态同步,看代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Attack : MonoBehaviour {
public float hp = 100;
public float normalAttack = 20;
public float attackDistance = 1;
protected Animator animator;
public float attackB = 80;
public float attackRange = 100;
public float attackGun = 100;
public AudioClip shotClip;
public AudioClip swordClip;
public GameObject player1;
//Attack a = new Attack();
void Start()
{
animator = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{

}
public void TakeDamage(float damage)
{
if (hp > 0)
{
hp -= damage;
}
else
{
animator.SetBool(“Dead”, true);
}
}
public void AttackA()
{
AudioSource.PlayClipAtPoint(swordClip, transform.position, 1f);
float distance = attackDistance;
GameObject enemy = player1;
if (enemy!=null)
{
Vector3 targetPos = enemy.transform.position;
targetPos.y = transform.position.y;
transform.LookAt(targetPos);
TakeDamage(normalAttack);
}
}
public void AttackB()
{
AudioSource.PlayClipAtPoint(swordClip, transform.position, 1f);
float distance = attackDistance;
GameObject enemy = player1;
if (enemy != null)
{
Vector3 targetPos = enemy.transform.position;
targetPos.y = transform.position.y;
transform.LookAt(targetPos);
TakeDamage(normalAttack);
}
}
public void AttackRange()
{
AudioSource.PlayClipAtPoint(swordClip, transform.position, 1f);
float temp = Vector3.Distance(player1.transform.position, transform.position);
if (temp < attackDistance)
{
TakeDamage(attackRange);
}
}
// Use this for initialization
}

 

using UnityEngine;
using System.Collections;

public class ATKAndDamage : MonoBehaviour {

public float hp = 100;
public float normalAttack = 50;
public float attackDistance = 1;
//public GameObject PanelPopup;
protected Animator animator;
GameManager a;
//public AudioClip deathClip;

protected void Awake() {
animator = this.GetComponent<Animator>();
a = GameObject.Find(“GameManager”).GetComponent<GameManager>();
}

public virtual void TakeDamage(float damage) {
if (hp > 0) {
hp -= damage;
}

if (hp > 0) {
if (this.tag == Tags.soulBoss || this.tag == Tags.soulMonster) {
animator.SetTrigger(“Damage”);
}
} else {

animator.SetBool(“Dead”, true);

// AudioSource.PlayClipAtPoint(deathClip, transform.position, 0.5f);
if (this.tag == Tags.soulBoss || this.tag == Tags.soulMonster) {
SpawnManager._instance.enemyList.Remove(this.gameObject);
SpawnAward();

Destroy(this.gameObject);
this.GetComponent<CharacterController>().enabled = false;
}
}

if (this.tag == Tags.soulBoss) {//当前受到攻击的对象是Boss
GameObject.Instantiate(Resources.Load(“HitBoss”), transform.position + Vector3.up, transform.rotation);
} else if (this.tag == Tags.soulMonster) {
GameObject.Instantiate(Resources.Load(“HitMonster”), transform.position + Vector3.up, transform.rotation);
}
}

void SpawnAward() {
int count = Random.Range(1, 3);
for (int i = 0; i < count; i++) {
int index = Random.Range(0, 2);
if (index == 0) {
GameObject.Instantiate(Resources.Load(“Item-DualSword”), transform.position + Vector3.up, Quaternion.identity);
} else if (index == 1) {
GameObject.Instantiate(Resources.Load(“Item-Gun”), transform.position + Vector3.up, Quaternion.identity);
}
}
}

private void LateUpdate()
{
if(GameObject.FindGameObjectsWithTag(Tags.soulBoss).Length == 0 &&
GameObject.FindGameObjectsWithTag(Tags.soulMonster).Length == 0&&
SpawnManager._instance.Last)
a.show();
}
}

 

using UnityEngine;
using System.Collections;

public enum AwardType
{
Gun,
DualSword
}

public class AwardItem : MonoBehaviour
{
public AwardType type;
public float speed = 8;
private bool startMove = false;
private Transform player;
public AudioClip pickupClip;

void Start()
{
//GetComponent<Rigidbody>().velocity = new Vector3(Random.Range(-5, 5), 0, Random.Range(-5, 5));
Vector3 position = new Vector3(Random.Range(-3, 3), 0, Random.Range(-3, 3));
transform.localPosition = position;
}

void Update()
{
if (startMove)
{
transform.position = Vector3.Lerp(transform.position, player.position + Vector3.up, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, player.position + Vector3.up) < 0.5f)
{
Debug.unityLogger.Log(2);
//TODO:没有加武器

player.GetComponent<PlayerAward>().GetAward(type);
Debug.unityLogger.Log(1);
//Destroy(this.gameObject);
Destroy(gameObject);
// AudioSource.PlayClipAtPoint(pickupClip, transform.position, 1f);
}
}
}

void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == Tags.ground)
{
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().isKinematic = true;
//GetComponent<Rigidbody>().velocity = Vector3.zero;
SphereCollider col = this.GetComponent<SphereCollider>();
col.isTrigger = true;
col.radius = 2;
}
}

void OnTriggerEnter(Collider col)
{
if (col.tag == Tags.player)
{
startMove = true;
player = col.transform;
}
}
}

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

%d 博主赞过: