0
\$\begingroup\$

As a total beginner, I've been working on a patch-job script for a gravity mechanic in an open space sim, I want to derive all my objects from this "Block ALPHA" object and its' set of variable attributes, so it's important that I get it right before moving on.

The idea is simple enough, call an array of all interactive objects' coordinates and apply a basic equation to simulate gravitational pull.

After some tinkering I have a piece of code that ticks all the boxes AFAIK, but I can't for the life of me figure out the syntax error I've made... I've been up all night massaging the script to fit my needs, but in the best cases it only raises one error: "expected '}'"

Maybe I'm just tired, maybe I broke the code, but trying to place parentheses only seems to bring more errors :/

Here's the code as it is so far complete with the compiler error and suggested fix (in red), which does no good whatsoever:

using UnityEngine;
using System.Collections;

public class DynamicWeightAnalysis : MonoBehaviour 
{

//Strength of attraction from your game-object, ideally this will be derived from a calculation involving the objects volume and density eventually
public float RelativeWeight;

//Here, we name our target objects
GameObject[] blockALPHA;

//Initialise code:
void Start () 
{
    //here, we define our target objects further by searching for the predefined tag
    blockALPHA = GameObject.FindGameObjectsWithTag("Block ALPHA");
}

//Use FixedUpdate because we are controlling the orbit with physics
void FixedUpdate ()
{ "} EXPECTED"

    //this is where we define the graverage coordinates
Vector3 graverage (Vector3[] blockALPHA)
    {
        if (blockALPHA.Length == 0)
            return Vector3.zero;
        float x = 0f;
        float y = 0f;
        float z = 0f;
        foreach (Vector3 pos in blockALPHA)
        {
            x += pos.x;
            y += pos.y;
            z += pos.z;
        }
        return new Vector3(x / blockALPHA.Length, y / blockALPHA.Length, z / blockALPHA.Length);

        Vector3 offset;

    //here we get the offset between the object and the average position of all objects
    offset = graverage - transform.position;

    //This is the variable for square magnitude calc
    float magsqr;

    //Offset Squared:
    magsqr = graverage.sqrMagnitude;

    //Check distance is more than 1 to prevent division by 0 (because my blocks are all 1x1x1, any closer and they'd be intersecting)
    if (magsqr > 1f)
        {
        //Create the gravity- make it realistic through division by the "magsqr" variable

            GetComponent<Rigidbody>().AddForce((RelativeWeight * graverage.normalized / magsqr) * GetComponent<Rigidbody>().mass);
        }
    }
}

I don't doubt that I've overlooked something stupid, but it's my first stab at proper coding, so, no shame in asking

Thanks :D

\$\endgroup\$
1
\$\begingroup\$

Are you calling FixedUpdate() or defining it? I think you should delete the opening brace after FixedUpdate(), and add a ; directly after FixedUpdate().

In other words, if your intent is to call the method, you should write

FixedUpdate ();

If your intent is to define a new method called FixedUpdate (), you should write

void FixedUpdate(){ //code// }

\$\endgroup\$
  • \$\begingroup\$ Thanks for the suggestion Jonah, I have tried that, it just generates a replacement error message: " Error CS0501: 'DynamicWeightAnalysis.FixedUpdate()' must declare a body because it is not marked abstract, extern, or partial (CS0501) (Assembly-CSharp)" My assumption, was that the code is supposed to be called from within the fixedupdate() {box} for per-frame processing, omitting the code defining "graverage" entirely brings me back to a workable, error free script, and it is a copy/paste mash-up, I guess I've made a small but impacting formatting error somewhere in that body of code. \$\endgroup\$ – Vinny Real Hard Ink Vince May 21 '16 at 7:11
  • \$\begingroup\$ So then you are in fact intending to define a new method called FixedUpdate ()? If so, are you allowed to declare another function inside the definition of FixedUpdate ()? \$\endgroup\$ – Jonah Havel May 21 '16 at 7:15
  • \$\begingroup\$ I'm sorry, I'm not 100% sure on the terminology, but yes, I think I'm supposed to be defining a method. If the problem were declaration of function within the FixedUpdate step, would I not have encountered the issue earlier when placing 'vector3 offset;' in the same block? \$\endgroup\$ – Vinny Real Hard Ink Vince May 21 '16 at 7:34
  • \$\begingroup\$ No, that is a variable declaration. Also, I think your function returns at the return statement every time, as written. Look, tomorrow if thus isn't answered I will rewrite the code. There are a number of "formatting" errors here, but it's a bit late for me to get on my laptop. \$\endgroup\$ – Jonah Havel May 21 '16 at 7:40
  • \$\begingroup\$ Thanks! I'll keep plugging at it, I've had a few ideas for improvements, and I'm working through some tuts which may (will surely) yield some results. I really appreciate the help, my background is not coding (obv). I haven't even attempted to write a script since my last .bat file to get doom2 running in hi-mem, so my existing knowledge base is, umm, rusty... :P \$\endgroup\$ – Vinny Real Hard Ink Vince May 21 '16 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.