【问题标题】:Jumping Perpendicular to rotating Object垂直于旋转物体跳跃
【发布时间】:2020-08-26 08:35:29
【问题描述】:

我有一个简单的跳跃游戏。在这个游戏中,有旋转平台和一个玩家对象。

每当玩家点击鼠标按钮时,它应该跳跃并粘在下一个平台上并随之旋转,直到他再次点击。我希望游戏对象垂直于旋转平台跳跃。

如果我使用 Vector3.up,游戏对象会倒下。但我希望玩家跳向绿色箭头的方向并坚持到下一个平台。

我在这里发帖,因为我在 2 周前在 Unity Forms 上发帖,但仍然没有得到答复。

TLDR:

这是我最近的工作: 我的播放器代码:

Rigidbody2D Rig;
public float Force =500;
public bool gamejump = true;
public Transform platformParent;   
bool playerforce = false;
bool setpos = false;
Vector2 pos = new Vector2(0, 0);
public Collider2D Ccollider;
public bool bottom =false;
void Start()
{
    Rig = GetComponent<Rigidbody2D>();
    Ccollider = GetComponent<CircleCollider2D>();
}

private void FixedUpdate()
{
      if (gamejump == true)
      {
          transform.SetParent(null);
          Rig.isKinematic = false;
          setpos = false;
      }
      else
      {
          transform.SetParent(platformParent);
          Rig.AddForce(new Vector2(0, 0));
          Rig.isKinematic = true;
          if (setpos == false)
          {
              setpos = true;
              transform.position = pos;
          }
     }  
}

void OnTriggerStay2D(Collider2D other)
{
    if (other.tag == "Rotate")
    {
        //if (Input.GetKey(KeyCode.Space))  
        if (Input.GetMouseButton(0))
        {
            gamejump = true;
            if (bottom == true)
            {
                Rig.AddForce(other.transform.up * Force);
            }
            else
            {
                Rig.AddForce(other.transform.up * -Force);
            }
       
        }
    }
}

void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Rotate")
    {
        ContactPoint2D contact = collision.contacts[0];
        pos = contact.point;
        if (collision.contacts.Length>0)
        {
            bottom = true;
        }
        else
        {
            bottom = false;
        }
        gamejump = false;  
    }
}

}

和我的平台代码:

 public bool counterclockwise;
Transform player;
player2 playerCode;
public Collider2D collidPlatform;
private int speed=100;
// Start is called before the first frame update
void Start()
{      
    player = GameObject.Find("player").GetComponent<Transform>();
    playerCode = FindObjectOfType<player2>();
    if (counterclockwise)
        speed = -speed;
}
void FixedUpdate()
{
   
    //  float currentZ = transform.eulerAngles.z;
    /* if (Limit == true)
     {
         if (currentZ > 180)
         {
             currentZ = 180;
         }
         Vector3 newEuler = new Vector3(0, 0, currentZ);
         transform.eulerAngles = newEuler; 
     }
     //transform.Rotate(new Vector3(0, 0, speed * Time.deltaTime));
     }
*/
}
private void OnCollisionEnter2D(Collision2D collision)
{
    playerCode.platformParent = transform;
    playerCode.Ccollider = collidPlatform;
}

}

仍然得到疯狂的结果,突然玩家在空中旋转或不粘在平台上并摔倒或当它粘在平台上时,它 提高平台的速度(我知道这是因为刚体附着在平台上,但如果我将其移除并尝试手动控制它,它不会按我想要的方式工作,所以如果你能给我关于如何手动和不旋转平台的建议刚体,所以我可以控制速度。

【问题讨论】:

  • 我从 5 分钟看一眼就看到了几个问题:看起来你的游戏是 2D 的,但是你有几个地方提到了 3D 向量,包括 Vector3.up 并且你正在维护Vector3 pos 在您的脚本中。这是我要开始确保第 3 维中的某些东西不会把你搞砸的第一个地方。
  • 是的,我的游戏是 2d 的,在我的新代码中,我将所有内容都更改为 2D,但我仍然有问题

标签: c# unity3d


【解决方案1】:

如果你使用transform.parent让玩家成为平台的子对象,然后在玩家本地轴上使用transform.up跳跃,如果你想让玩家降落在他们跳跃的相同位置,玩家可以保持以旋转平台为父对象,否则您需要在跳跃后将播放器作为子对象移除到平台。但由于你使用的是刚体物理,我认为你会得到来自重力的混合结果。

【讨论】:

  • 是的,我试过了,但正如你提到的,我得到了奇怪的结果,我还尝试手动移除刚体并旋转平台,对于一些可以工作但玩家突然挂起的平台在空中或剂量不粘在下一个平台上,只是撞到平台并跌倒
猜你喜欢
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多