-torqueAmount OR torqueAmount = torqueamount - 1f?

Hi,

To get the example to work I had to write

    if(Input.GetKey(KeyCode.LeftArrow))
    {
        rb2d.AddTorque(torqueAmount = torqueAmount + 1f);
    }
    else if(Input.GetKey(KeyCode.RightArrow))
    {
        rb2d.AddTorque(torqueAmount = torqueAmount - 1f);
    }

instead of

    if(Input.GetKey(KeyCode.LeftArrow))
    {
        rb2d.AddTorque(torqueAmount);
    }
    else if(Input.GetKey(KeyCode.RightArrow))
    {
        rb2d.AddTorque(-torqueAmount);
    }

I am wondering that what I have done wrong or what is the concept I have not understood?

Hi Hirmu,

That’s odd. Actually, the code at the bottom should have worked. What value does/did torqueAmount have when the code was not working as expected? Maybe it was too low because in your first code, you add/subtract 1f, which might be the reason why your approach solved your problem while the other one did not.

And what do you mean by “not working”? What did you expect to happen? What happened instead?

The torque amount was 1 in the inspector view.

I was trying to get the snowboarder to roll around. With the upper example that happens just like in the tutorial video. With the latter example (copied from the resources) nothing happens to the torque amound in the inspector.

Test 2 in the Inspector.

And if that did not work either, log the value of torqueAmount into your console after you executed your working code. I’m relatively sure that the problem is caused by the value.

Unfortunately that did not help.

With rb2d.AddTorque(torqueAmount); and b2d.AddTorque(-torqueAmount); the inspector value Torque Amount does not change at all when in the play mode and pressing left and right arrows.

With the rb2d.AddTorque(torqueAmount = torqueAmount + 1f); and rb2d.AddTorque(torqueAmount = torqueAmount - 1f); version the inspector values changes OK and the snowboarder rolls backward and forward.

The code is as follows

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

public class PlayerController : MonoBehaviour
{
[SerializeField] float torqueAmount = 1f;
Rigidbody2D rb2d;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent();
}

// Update is called once per frame
void Update()
{ 
    
    if(Input.GetKey(KeyCode.LeftArrow))
    {
        rb2d.AddTorque(torqueAmount = torqueAmount + 1f);
    }
    else if(Input.GetKey(KeyCode.RightArrow))
    {
        rb2d.AddTorque(torqueAmount = torqueAmount - 1f);
    }
    

    /*if(Input.GetKey(KeyCode.LeftArrow))
    {
        rb2d.AddTorque(torqueAmount);
    }
    else if(Input.GetKey(KeyCode.RightArrow))
    {
        rb2d.AddTorque(-torqueAmount);
    }
    */

}

}

The console view shows that it registers the keypresses in both cases. In the other case the value just does not change.

In this other case that does not work there is also another interesting point. Dragging the Torque amount in Inspector does not make any kind of effect either to the snowboarder behaviour.

Which version of Unity do you use? I’m wondering if there might be a bug in your version because the behaviour is really strange.

Maybe you could try to remove the component from your game object and readd it. In rare cases, the components are buggy.

I’m back on Monday. If you are in a hurry, please feel free to ask our helpful community of students over on our Discord chat server.

The version is 2021.3.5f1

As the workaround seems to function OK this is not urgent issue. More just a thing that I think would be important to understand. So not in hurry :slight_smile:

I’m glad you found a solution. Feel free to keep it for now. :slight_smile:

Could you please upload your project to GitHub without the Temp and Library folder and share a link to the public repository here? I’d like to take a look into this next week. Maybe I will be able to find the reason why Rick’s code did not work for you. I cannot promise anything, though, because I have never encountered this issue before and do not know if I’ll be able to recreate it on my computer.

Here is a tutorial video by Brackeys:

If everything went OK the project should be available (without the Temp and Library folders) at https://github.com/HirmuHirmu/Unity-snowboarding-game-tutorial

Thank you. It worked. I had to import it into Unity 2021.1.26f1 because I currently cannot install and run other versions.

The first thing I noticed is that Barry’s movement is very laggy. To fix this problem, I set the “Interpolate” field to “Interpolate” in his Rigidbody2D component. And I set the “Update Method” of the CinemachineBrain component to “Late Update”.

This is your code from the repository:

        if(Input.GetKey(KeyCode.LeftArrow))
        {
            rb2d.AddTorque(torqueAmount++);
        }
        else if(Input.GetKey(KeyCode.RightArrow))
        {
            rb2d.AddTorque(torqueAmount--);
        }

Barry rotated for me but the result looked “funny”. I replaced this code with:

        if(Input.GetKey(KeyCode.LeftArrow))
        {
            rb2d.AddTorque(torqueAmount);
        }
        else if(Input.GetKey(KeyCode.RightArrow))
        {
            rb2d.AddTorque(-torqueAmount);
        }

And that fixed the problem for me in my version of Unity.

So, in the end, I was not able to recreate your problem. Maybe, if you have time, you could install Unity 2022.1.7 and test your project with the code that did not work in 2021.3.5f1. If the issue persists, please report a bug to Unity via Help > Report a bug (in the Unity Editor).

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

Privacy & Terms