I want My own controller

here is my code(attached with Player)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControll : MonoBehaviour
{
[SerializeField] float torque = 1.0f;

[SerializeField] GameObject surface;
Rigidbody2D rb2d;
SurfaceEffector2D efect2d;
// Start is called before the first frame update
void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
    efect2d = surface.GetComponent<SurfaceEffector2D>();
}

// Update is called once per frame
void Update()
{
    
}



 void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log("What happning");
    if (collision.gameObject.tag == "Grounded")
        ForwardMovement();
    else
        FlipPlayer();
}






void FlipPlayer()
{
    //Rotating
    if (Input.GetKey(KeyCode.RightArrow))
    {
        rb2d.AddTorque(torque);
    }
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        rb2d.AddTorque(-torque);
    }
}

//Movement Forward and Backward
void ForwardMovement()
{
    if (Input.GetKey(KeyCode.RightArrow))
    {
        efect2d.speed += 0.08f;
    }
    else
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        efect2d.speed -= 0.08f;
    }
    else
    {
        if (efect2d.speed < 0)
        {
            efect2d.speed += 0.1f;
        }
        else
        if (efect2d.speed > 0)
        {
            efect2d.speed -= 0.1f;
        }
    }
}

}

I want to do is when player is grounded my input work to speed-up player and when-ever it is in the air same will be work for flipping player

it works as if I put this two methods in update but in OnCollisionEnter2d it is not working how to solve this (I am thinking OnCollisionEnter2d is called once only when collision happens ) Is there alternative or just wrong I am ?

hey guys !
Solved this here is code its amazing
and I want to post gameplay video how to do ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControll : MonoBehaviour
{
[SerializeField] float torque = 1.0f;

[SerializeField] GameObject surface;
Rigidbody2D rb2d;
SurfaceEffector2D efect2d;
bool isInAir = false;
// Start is called before the first frame update
void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
    efect2d = surface.GetComponent<SurfaceEffector2D>();
}

// Update is called once per frame
void Update()
{
    if (!isInAir)
        ForwardMovement();
    else
        FlipPlayer();
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Grounded")
        isInAir = false;
}


private void OnCollisionExit2D(Collision2D collision)
{
    isInAir = true;
}







void FlipPlayer()
{
    //Rotating
    if (Input.GetKey(KeyCode.RightArrow))
    {
        rb2d.AddTorque(torque);
    }
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        rb2d.AddTorque(-torque);
    }
}

//Movement Forward and Backward
void ForwardMovement()
{
    if (Input.GetKey(KeyCode.RightArrow))
    {
        efect2d.speed += 0.08f;
    }
    else
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        efect2d.speed -= 0.08f;
    }
    else
    {
        if (efect2d.speed < 0)
        {
            efect2d.speed += 0.1f;
        }
        else
        if (efect2d.speed > 0)
        {
            efect2d.speed -= 0.1f;
        }
    }
}

}

Good job on solving the problem! :slight_smile:

Regarding your gameplay video, you could use OBS Studio to record a video, and you could upload that video to Youtube.

In Our Community we could or not upload video ?
that 's my question by the way , and sorry for bad english

Yes, you may upload your video here. The best would be to share your results in our Show subforum. Here’s the link:

Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms