有一点问题。我已经制作了一些没有错误的脚本,但是在Unity 3d中的精灵工厂中尝试启动攻击动画时遇到麻烦,在角色或敌人攻击时基本上是动画。他们都是骨头闲置。我已经在我的主角和敌人上附加了必要的rigidbody2d和盒子对撞机,但没有任何互动。我已经设法使用Sprite Factory(以下代码作为示例)为我的角色激活我的运行动画
using UnityEngine;
using System.Collections;
using FactorySprite = SpriteFactory.Sprite;
public class Walk : MonoBehaviour {
// you forgot to set name of variable representing your sprite
private FactorySprite sprite;
// Use this for initialization
void Start () {
sprite = GetComponent<FactorySprite> (); // Edited
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.RightArrow)) {
sprite.Play ("Walk"); // heh, remember C# is case sensitive :)
Vector3 pos = transform.position;
pos.x += Time.deltaTime * 12;
transform.position = pos;
} else if (Input.GetKey (KeyCode.LeftArrow)) {
sprite.Play ("Walk0"); // heh, remember C# is case sensitive :)
Vector3 pos = transform.position;
pos.x += Time.deltaTime * -12;
transform.position = pos;
}
else{
sprite.Stop();
}
}
}
脚本低于[玩家攻击]
using UnityEngine;
using System.Collections;public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 2.5f) {
if(direction > 0) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
球员健康:
using UnityEngine;
using System.Collections;
public class PlayersHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
敌人攻击
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 2.5f) {
if(direction > 0) {
PlayersHealth eh = (PlayersHealth)target.GetComponent("PlayersHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
敌人的健康
Using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
我会喜欢任何人的专业知识,因为我处于关卡的最后阶段,这确实造成了损失。