Jumping Perpendicular to rotating Object

Viewed 106

I have a simple jumping game. In this game there are rotating platforms, and a player object.

Whenever the player clicks the mouse button, it should jump and sticks to the next platform and rotates with it, until he clicks again. I want the game object to jump perpendicular to the rotating platforms.

If i use Vector3.up the game object will fall down instead. But I want the player to jump in the direction of a green arrow and stick to the next platform.

I'm posting here, because I've posted on the Unity Forms 2 weeks ago and still got no answer.

TLDR:

here is what i've worked on recently : my player code :

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;  
    }
}

}

and my platform code :

 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;
}

}

and still got crazy results, suddenly the player rotates in the air or dose not sticks to a platform and falls or when it sticks to a platform ,it Increases platform's speed (i know it's because of rigid body that attaches to platforms but if i remove it and try to control it manually it dose not work the way i want ,so if you could give me suggestion on how to rotate platforms manually and without rigid body so i be able to control the speed .

1 Answers

if you make the player a child object of the platform using transform.parent, then jump using transform.up on the players local axis, if you want the player to land on the same position where they jumped, the player can remain parented to the rotating platform, otherwise you need to remove the player as a child object to the platform after you jump. But since you are using rigidbody physics, I think you will get mixed results from gravity.

Related