C# 垂直于旋转物体跳跃

C# 垂直于旋转物体跳跃,c#,unity3d,C#,Unity3d,我有一个简单的跳跃游戏。在这个游戏中有旋转平台和一个玩家对象 每当玩家点击鼠标按钮时,它都会跳到下一个平台并随之旋转,直到再次点击为止。我希望游戏对象垂直于旋转平台跳跃 如果我使用Vector3.up,游戏对象将倒下。但我希望玩家朝着绿色箭头的方向跳,并坚持到下一个平台 我在这里发帖,因为我两周前在Unity表单上发帖,但仍然没有得到回复 TLDR: 以下是我最近的工作: 我的播放器代码: Rigidbody2D Rig; public float Force =500; public bool

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

每当玩家点击鼠标按钮时,它都会跳到下一个平台并随之旋转,直到再次点击为止。我希望游戏对象垂直于旋转平台跳跃

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

我在这里发帖,因为我两周前在Unity表单上发帖,但仍然没有得到回复

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;  
    }
}
Rigidbody2D钻机;
公众浮力=500;
公共bool gamejump=true;
公共转换平台;
bool playerforce=false;
bool setpos=false;
向量2位置=新向量2(0,0);
公共碰撞R2D准直器;
公共bool-bottom=false;
void Start()
{
Rig=GetComponent();
Ccollider=GetComponent();
}
私有void FixedUpdate()
{
如果(gamejump==true)
{
transform.SetParent(null);
Rig.iskinetic=false;
setpos=false;
}
其他的
{
transform.SetParent(platformParent);
新矢量2(0,0));
Rig.iskinetic=true;
如果(setpos==false)
{
setpos=true;
transform.position=pos;
}
}  
}
无效OnTiggerStay2D(碰撞的R2D其他)
{
如果(other.tag==“旋转”)
{
//if(Input.GetKey(KeyCode.Space))
if(输入。GetMouseButton(0))
{
gamejump=true;
如果(底部==真)
{
Rig.AddForce(other.transform.up*Force);
}
其他的
{
Rig.AddForce(其他.transform.up*-力);
}
}
}
}
空心OnCollisionInter2D(碰撞2D碰撞)
{
如果(collision.gameObject.tag==“旋转”)
{
ContactPoint2D contact=冲突。contacts[0];
pos=接触点;
如果(碰撞.触点.长度>0)
{
底部=真;
}
其他的
{
底部=假;
}
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;
}
public bool逆时针方向;
转换玩家;
player2播放代码;
公共碰撞R2D碰撞平台;
私人整数速度=100;
//在第一帧更新之前调用Start
void Start()
{      
player=GameObject.Find(“player”).GetComponent();
playerCode=FindObjectOfType();
如果(逆时针)
速度=-速度;
}
void FixedUpdate()
{
//float currentZ=transform.eulerAngles.z;
/*if(Limit==true)
{
如果(当前Z>180)
{
电流z=180;
}
Vector3 newEuler=新的Vector3(0,0,currentZ);
transform.eulerAngles=newEuler;
}
//旋转(新矢量3(0,0,速度*时间增量));
}
*/
}
专用空心OnCollisionInter2D(碰撞2D碰撞)
{
playerCode.platformParent=转换;
playerCode.Ccollider=碰撞平台;
}
}

但仍然得到了疯狂的结果,突然玩家在空中旋转,或者没有粘在平台上摔倒,或者当它粘在平台上时,它就倒下了
提高平台的速度(我知道这是因为附着在平台上的刚体,但如果我移除它并尝试手动控制它,它将无法按照我想要的方式工作,因此,如果你能给我建议,如何手动旋转平台,而不使用刚体,以便我能够控制速度。

如果你使用
tr将播放器作为平台的子对象ansform.parent
,然后使用玩家本地轴上的
transform.up
进行跳跃,如果你想让玩家降落在他们跳跃的相同位置,玩家可以保持旋转平台的父级,否则你需要在跳跃后将玩家作为子对象移除到平台上。但是,因为你使用的是刚体物理cs,我想你会从重力中得到好坏参半的结果。

从5分钟的一瞥中我看到了几个问题:你的游戏似乎是2D的,但你有几个地方提到了3D向量,包括Vector3.up,你在脚本中维护了一个
Vector3 pos
。这是我第一个开始确保是的,我的游戏是2d的,在我的新代码中,我把所有东西都改成了2d,但仍然有问题是的,我试过了,但正如你提到的,我得到了很好的结果,我还尝试移除刚体并手动旋转平台,对于一些工作正常的平台,但突然播放器挂在空中或dos上e不要粘在下一个平台上,而是撞到平台上摔倒