我正在為一個學校專案開發一個小游戲,我的玩家需要在一個關卡中攻擊敵人。我的計劃是在附加的腳本中啟用一個對撞機,然后在攻擊完成時禁用它。我目前的問題是,對撞機沒有按照預期的方式翻轉,它似乎直接在整個 x 軸上翻轉,而不是在與玩家相關的 x 軸上翻轉。它是玩家的孩子,所以我不知道它為什么這樣做。任何解決方案或其他方法將不勝感激。我將在下面附上控制對撞機的當前腳本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VerticalSword : MonoBehaviour
{
//Variables use to active the colliders
Collider2D AttackColliderVertical;
//Variables for the location of the collider
Vector2 attackOffset;
private void Start()
{
AttackColliderVertical = GetComponent<Collider2D>();
attackOffset = transform.position;
AttackColliderVertical.enabled = false;
}
public void FixedUpdate()
{
attackOffset = transform.position;
}
public void AttackUp()
{
AttackColliderVertical.enabled = true;
if (attackOffset.y > 0)
{
transform.position = attackOffset;
}
else if (attackOffset.y < 0)
{
transform.position = new Vector2(attackOffset.x, (attackOffset.y * -1)); //I think the problem is somewhere in this if and else if statement
}
print("Attack up successful"); //Error checking (This works when run)
}
public void AttackDown()
{
AttackColliderVertical.enabled = true;
if (attackOffset.y > 0)
{
transform.position = new Vector2(attackOffset.x, (attackOffset.y * -1));
}
else if (attackOffset.y < 0)
{
transform.position = attackOffset; //I think the problem is somewhere in this if and else if statement
}
print("Attack down successful"); //Error checking (This works when run)
}
public void StopAttack()
{
AttackColliderVertical.enabled = false;
}
}
uj5u.com熱心網友回復:
使用 transform.localPosition,而不是 transform.position(這是它的世界空間位置)。您需要在此腳本中的任何地方更改它;Start() 函式和兩個攻擊函式
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/521516.html
標籤:unity3d
下一篇:如何將特定物件重置為其原始旋轉?