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 ?