Having trouble with AddTorque script (C#)

Hey guys, first off. I’m kinda new to programming. I’ve just followed a couple of lessons at my school, trying to learn it and all.

I’m trying to achieve something but it isn’t exactly working out with the errors that unity is giving me. Since I’ve been looking at the code for over an hour now and I just can’t seem to sort it out. Also, by having an early weekend I can’t really get in touch with my teacher 'till monday. I’d really appreciate it if you guys could help me out.

What I want it to do, which you can probably already tell by my amaturish coding, is that when you press Q, it adds force and turns one way. And when you press A, it adds force the other way which will make it stop and turn the other way/ vice versa.


using UnityEngine;
using System.Collections;

public class RotationA : MonoBehaviour

{
	void FixedUpdate ()
	{

	if(Input.GetKeyDown(KeyCode.Q))
	
			// Spins the rigidbody around axis one way

			function ()	{
		rigidbody.AddTorque (Vector3.up * 10);
		}
			



	if(Input.GetKeyDown(KeyCode.A))

		// Spins the rigidbody around axis the other way

		function ()	{
		rigidbody.AddTorque (Vector3.up / 10);
		}
	}
}

And these are the errors the console is giving me:

RotationA.cs (14.41): error CS1525: Unexpected symbol ‘{’

RotationA.cs (10.9): warning CS0642: Possible mistaken empty statement

RotationA.cs (19.1): error CS8025: Parsing Error


          Edit after the answer:

Hmm, well as for the function. I now realise that was javascript. I mixed them up, kinda silly of me.

As far as the code, it seems to work so thanks for that :smiley: really appreciate you taking the time to correct it :slight_smile: But it doesn’t actually do what I’m trying to achieve. I probably missthink it, and I didn’t actually explain it right in my first post.


alt text

As written in the text, the wheel doesn’t stick to the bottom of the moving arm. Also, the arm doesnt follow its local axis right, even if its on the good spot.
I don’t really know why this happens.

Also, as you can see by the picture on the top right. I want it just to make a circular motion along the Y axis with the wheel attached. But in a way both objects can turn individually. Its like a funfair ride ( reference ).

As far as I’ve got myself… I found the right ‘command’ AddRelativeTorque instead of just the AddTorque, which made the arm go spin around like its nuts and adjusted the up into a forward to make it turn along the right axis.

Any idea how I can solve this? Thanks alot.


using UnityEngine;
using System.Collections;

public class RotationA : MonoBehaviour

{
	void FixedUpdate ()
	{

		if(Input.GetKeyDown(KeyCode.Q))

			// Spins the rigidbody around local¨ axis one way

		{ 
			rigidbody.AddRelativeTorque (Vector3.forward * 10 );
		}




		if(Input.GetKeyDown(KeyCode.A))
			// Spins the rigidbody around local¨ axis the other way
		{
			rigidbody.AddRelativeTorque (Vector3.forward / 10 );
		}
	}
}

I’m not sure what you think you are doing with function ()

public class RotationA : MonoBehaviour

{
    void FixedUpdate ()
    {

    if(Input.GetKeyDown(KeyCode.Q))

            // Spins the rigidbody around axis one way

        {
           rigidbody.AddTorque (Vector3.up * 10);
        }




    if(Input.GetKeyDown(KeyCode.A))
        // Spins the rigidbody around axis the other way
        {
           rigidbody.AddTorque (Vector3.up / 10);
        }
    }
}

Maybe check out some C# basics if and switch statements - select a code path to execute | Microsoft Learn