![](/img/trans.png)
[英]Unity - How to check if there is a Tile of an specific Tilemap in the position x,y,z
[英]Unity C# instantiating “position + x, y, z”?
因此,我有一个使用武器的角色,目前将其设置为在角色的位置生成。 我希望该位置成为我的角色,再加上一点点右边(x轴)。 我不知道如何执行此操作,也无法在Google上找到答案。 我是初学者,请具体说明。 这是我的代码:
public float speed = 2f;
Animator anim;
private Rigidbody2D rb2d;
public float jumpHeight = 20f;
public GameObject weapon;
private Vector2 playerPos;
public GameObject player;
private void Start()
{
anim = GetComponent<Animator>();
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
playerPos = player.transform.position;
if (Input.GetKey(KeyCode.D))
{
anim.SetInteger("State", 1);
transform.Translate(new Vector2(1f * speed * Time.deltaTime, 0f));
}
if (Input.GetKeyUp(KeyCode.D))
{
anim.SetInteger("State", 3);
}
if (Input.GetKey(KeyCode.A))
{
anim.SetInteger("State", 2);
transform.Translate(new Vector2(-1f * speed * Time.deltaTime, 0f));
}
if (Input.GetKeyUp(KeyCode.A))
{
anim.SetInteger("State", 4);
}
if (Input.GetKey(KeyCode.P))
{
rb2d.AddForce(Vector2.up * jumpHeight);
}
if (Input.GetKeyDown(KeyCode.O))
{
Instantiate(weapon, playerPos, Quaternion.Euler(0, 0, -40));
}
}
在Unity中, Transform.position
是一个Vector3
。 您可以将两个Vector3
与+
运算符一起添加:
Vector3 result = myVectorA + myVectorB
// or
Vector3 result = new Vector3(5,6,7) + new Vector3(10,0,0) // result is (15,6,7)
要添加到playerPos的x
值-您需要添加一个完整的Vector3,该向量具有必要的转换,因为它是x
值。
playerPos + new Vector3(100, 0, 0)
有关Unity向量算法的详细信息,请参见以下网址 : https : //docs.unity3d.com/Manual/UnderstandingVectorArithmetic.html
而关于Unity的答案说明了为什么您不能同时直接直接分配到playerPos.x
: http : playerPos.x
in.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.