【发布时间】:2020-03-16 16:08:37
【问题描述】:
我正在努力学习 Unity。我的目标是建造一个小行星克隆体。
目前,我的角色能够向鼠标位置飞行、射击和旋转。然而,子弹会以某种方式受到玩家转动的影响。
这里是一些示例代码:
player.cs
private void Shoot()
{
Instantiate(Bullet, transform);
}
private void FaceMouseDirection()
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.back);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turningSpeed * Time.deltaTime);
}
bullet.cs
private GameObject parent;
private Vector3 Force;
// Use this for initialization
void Start ()
{
parent = GameObject.Find("Player");
Force = parent.transform.up * bulletSpeed;
}
void FixedUpdate()
{
transform.Translate(Force * Time.fixedDeltaTime);
}
}
【问题讨论】:
-
当一个游戏对象是另一个对象的子对象时,它使用父对象变换。它自己的变换变得相对于父级。只是不要设置父母,你很高兴
标签: c# unity3d game-development