子对象相对于父对象的旋转
Child object rotation relative to parent
我一直在尝试让玩家的枪靶与玩家一起旋转。我的球员一直向右侧射门,即使他面向左侧。
我创建了一个名为 Firepoint 的目标,它被设置为玩家的子对象。我希望它随着角色改变方向而改变。
任何帮助表示赞赏。
这是播放器的代码。
public void Move()
{
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
myRigidBody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("Walking", playerHasHorizontalSpeed);
}
public void Flipsprite()
{
bool playerhashorizontalspeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
if (playerhashorizontalspeed)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
transform.rotation = new Vector2(Mathf.Sign(firePoint.transform.localScale.x), 1f);
}
}
这里的问题是旋转不受缩放影响。只有相对比例和位置。
还有你为什么使用 > Mathf.Epsilon
而不是简单的 > 0
。据我了解,您只需要检查它是否不为 0 ...使用 Mathf.Epsilon
的差别很小,实际上并不重要。
public void Move()
{
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
myRigidBody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > 0;
myAnimator.SetBool("Walking", playerHasHorizontalSpeed);
}
// store the last direction
int direction;
public void Flipsprite()
{
bool playerhashorizontalspeed = Mathf.Abs(myRigidBody.velocity.x) > 0;
if (playerhashorizontalspeed)
{
// update the direction
direction = Mathf.Sign(myRigidBody.velocity.x);
transform.localScale = new Vector2(direction, 1f);
}
}
然后当你拍摄时,方向也乘以 direction
private IEnumerator FireContinuously()
{
while (true)
{
GameObject laser = Instantiate(bullet, firePoint.position, firePoint.rotation);
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(projectileSpeed * direction, 0);
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
一个小提示:
如果你制作
的类型
public RigidBody2D bullet
再拖入对应的prefab,不用GetComponent
直接用
var laser = Instantiate(bullet, firePoint.position, firePoint.rotation);
laser.velocity = ...
我一直在尝试让玩家的枪靶与玩家一起旋转。我的球员一直向右侧射门,即使他面向左侧。 我创建了一个名为 Firepoint 的目标,它被设置为玩家的子对象。我希望它随着角色改变方向而改变。 任何帮助表示赞赏。
这是播放器的代码。
public void Move()
{
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
myRigidBody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("Walking", playerHasHorizontalSpeed);
}
public void Flipsprite()
{
bool playerhashorizontalspeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
if (playerhashorizontalspeed)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
transform.rotation = new Vector2(Mathf.Sign(firePoint.transform.localScale.x), 1f);
}
}
这里的问题是旋转不受缩放影响。只有相对比例和位置。
还有你为什么使用 > Mathf.Epsilon
而不是简单的 > 0
。据我了解,您只需要检查它是否不为 0 ...使用 Mathf.Epsilon
的差别很小,实际上并不重要。
public void Move()
{
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
myRigidBody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > 0;
myAnimator.SetBool("Walking", playerHasHorizontalSpeed);
}
// store the last direction
int direction;
public void Flipsprite()
{
bool playerhashorizontalspeed = Mathf.Abs(myRigidBody.velocity.x) > 0;
if (playerhashorizontalspeed)
{
// update the direction
direction = Mathf.Sign(myRigidBody.velocity.x);
transform.localScale = new Vector2(direction, 1f);
}
}
然后当你拍摄时,方向也乘以 direction
private IEnumerator FireContinuously()
{
while (true)
{
GameObject laser = Instantiate(bullet, firePoint.position, firePoint.rotation);
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(projectileSpeed * direction, 0);
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
一个小提示:
如果你制作
的类型public RigidBody2D bullet
再拖入对应的prefab,不用GetComponent
直接用
var laser = Instantiate(bullet, firePoint.position, firePoint.rotation);
laser.velocity = ...