C# Unity2D快照对象随角色旋转,但不应
我在努力学习团结。我的目标是建立一个小行星克隆 目前,我的角色可以朝鼠标位置飞行、射击和旋转。然而,子弹在某种程度上受到玩家转向的影响 以下是一些示例代码: player.csC# Unity2D快照对象随角色旋转,但不应,c#,unity3d,game-development,C#,Unity3d,Game Development,我在努力学习团结。我的目标是建立一个小行星克隆 目前,我的角色可以朝鼠标位置飞行、射击和旋转。然而,子弹在某种程度上受到玩家转向的影响 以下是一些示例代码: player.cs private void Shoot() { Instantiate(Bullet, transform); } private void FaceMouseDirection() { Vector2 direction = Camera.main
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);
}
private void Shoot()
{
GameObject bullet = Instantiate(Bullet, transform.position, transform.rotation);
}
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);
}
}
private Vector3 Force;
// Use this for initialization
void Start ()
{
Force = -transform.right * bulletSpeed;
}
void FixedUpdate()
{
transform.Translate(Force * Time.fixedDeltaTime);
}
}
子弹被设定为玩家的孩子。子对象与其父对象保持相同的旋转 在项目符号的开始处,您将希望在设置力后从项目符号中移除父项 在我脑子里,你应该可以用
transform.parent = null;
这可能是因为您将项目符号实例化为对象变换的子对象。 然后,当对象旋转时,项目符号也会旋转 您希望在world/root中实例化它们,或者如果希望在层次结构中对它们进行分组,则可以在0,0,0的静态对象中实例化它们
Object.instantialebullet.transform.position=base.transform.position首先,不要让子弹成为玩家的子项。不要使用沸点、变换 第二,当子弹被实例化时,将子弹的旋转和位置设置为与玩家相同。不要使用沸点、transform.position、transform.rotation 第三,不要将子弹的移动建立在玩家变换的基础上。只需使用它自己的转换。不要使用Force=transform.up*bulletSpeed;而不是Force=parent.transform.up*bulletSpeed 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);
}
private void Shoot()
{
GameObject bullet = Instantiate(Bullet, transform.position, transform.rotation);
}
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);
}
}
private Vector3 Force;
// Use this for initialization
void Start ()
{
Force = -transform.right * bulletSpeed;
}
void FixedUpdate()
{
transform.Translate(Force * Time.fixedDeltaTime);
}
}
第四,你应该考虑使用一种调用你的子弹的技术,因为实例化和破坏游戏对象的许多近乎重复的东西通常不是非常有效的。
注意:如果在项目符号上使用刚体或刚体2d,则应使用rb.MovePositiontransform.position+Force*Time.deltaTime;Time.deltaTime与FixedUpdate中的Time.fixedDeltaTime相同,因为它将为在调用FixedUpdate之间渲染的帧正确插值 当一个游戏对象是另一个对象的子对象时,它使用父对象变换。其自身的变换将相对于父对象。只是不要把父母放在一边,你会很好地抓住汉克斯,这样做很有效。子弹飞得很奇怪,因为它们不会沿着绿轴单独飞行。。但我想这是离题了。非常感谢分享!你知道为什么我的子弹飞行被抵消了吗?当玩家朝北看的时候,公牛会飞到那里。但当玩家向东看时,子弹会向南飞…@NullOrNotNull,因为子弹的旋转方式使其局部向上移动的方向不正确。从你的评论来看,这听起来好像他们真的应该沿着他们当地的左方向移动,但是如果没有显示他们当地轴和他们要移动的方向的屏幕截图,就很难确定。无论如何,我编辑了我答案的bullet.cs部分。如果这对你有什么影响,请告诉我。实际上,对于子弹,我只需将其设置为rb.velocity=-transform.right*bulletSpeed;只使用一次更新或修复更新:@derHugo是的,这可能更好: