Addind Torque

there has been problem with my game play

Hi,

Could you please elaborate further on what you did/have in Unity and on your problem? What did you expect to happen? What happened instead? Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? And have you already rewatched the video at least one more time?

legging while playing
movement of player is not like continues it is distracting
comparing to lecture’s code I am using input system to control my player 's speed and code is here

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

public class PlayerControll : MonoBehaviour
{
[SerializeField] float torque = 1.0f;
[SerializeField] float speed = 0;
[SerializeField] GameObject surface;
Rigidbody2D rb2d;
SurfaceEffector2D efect2d;
bool isInAir = false;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent();
efect2d = surface.GetComponent();
}

// 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);
    }
    else
    {
        if (efect2d.speed < 0)
        {
            efect2d.speed += speed;
        }
        else
        if (efect2d.speed > 0)
        {
            efect2d.speed -= speed;
        }
    }
}

//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 += speed;
        }
        else
        if (efect2d.speed > 0)
        {
            efect2d.speed -= speed;
        }
    }
}

}

In the Rigidbody2D component in the Inspector, did you set the Interpolate value to “Interpolate” or “Extrapolate”? If not, do that please, and test your game again.

1 Like

Done ! thanks for being supportive

You’re welcome. :slight_smile:

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

Privacy & Terms