C# how many voids ,,,standard practice...?

I am trying to understand the actual void process etc. Meaning, if i create a void fixed update, for say a player controller, and include with that the collection and update of the items collected on screen,
do I need to add another fixed update to create another action for the controller, such as a jump function, then another update for a strafe function. It seems as if I could include this in one fixed update or void fixed update etc, but the code would look to me to become a bit monstrous in code lines.

What is the simplest and easiest to read standard method of doing this sort of thing. As currently I am trying to add a jump in roll-a-ball, but having to mix up more getcomponent callings and similar code requests etc.

I mean I wouldn’t want 100 void fixed updates or one large code block. Do I create more than one script for the player controller? or the rigidbody? getting a bit lost and this is only the beginning :frowning: will probably have to buy a unity book to get any sense other than code snippets from unity docs…

Hmm. Well, you wouldn’t put more than 1 FixedUpdate into a single script.

It’s best to try to imagine grouping your related things in 1 script, and if it’s something different, in a new script. That’s a simple way to think of it.
I would say that jumping and movement can easily be understood as related. Sure, you could use 2 scripts, but I think 1 makes sense.

void is used for functions that don’t return anything so depending on the size and scope you could have 1000’s of void whatever () classes.

void DoStuff() { }

void DoMoreStuff() {}

int ReturnStuff() { return 2 + 2; }

public class example : MonoBehaviour

{
//Variables

public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;

void Update()

{
CharacterController controller = GetComponent();

// is the controller on the ground?

if (controller.isGrounded)
{
//Feed moveDirection with input.

moveDirection = new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);

//Multiply it by speed.
moveDirection *= speed;

//Jumping
if (Input.GetButton(“Jump”))
moveDirection.y = jumpSpeed;

}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}

This is the unity basic controller, so what I’m getting at, is where do I put more variables for say, throwing a grenade with animation. At the very beginning? then create a void fixed update adding a new code block, or do I mix it with the code shown here in void update/just changing it to fixed?

If I add strafe functions and extra boost variable for speed or sonic style spins do I add variables for this :

now… or at beginning?

Do I have to just write out the full functions & variables and desired actions before I even start coding, on paper, so this issue is dealt with before coding? Do I have a list of 500 variables at the beginning or in seperate blocks for every new function?

creating a character with new actions would seem to be a nightmare, fitting in the new variables and function code? Is it?

I’d probably put a grenade in a different script, but strafing and what not can go in there, sure.

FixedUpdate is for physics, so if you use that, then that’s a different story.

I am not sure I always follow your questions. You should probably try to slow down a little …
I don’t think you’ll have 500 variables in this script, for example :slight_smile:

You want to declare variables outside of methods, if they’re going to be used in other places. Otherwise, you want to declare them in methods (locally).
It’s a good idea for a method to deal with a task, especially if the method can be re-used.

I think you should try working on it a bit longer, and set aside your huge wonder about what it’s going to be, because you may end up wondering indefinitely :slight_smile:

Thanks, I think i’ll just focus on a new tutorial problem, hopefully someone will help…

public class PlayerController : MonoBehaviour
{
public float speed;

private void FixedUpdate() //for physics…
{
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
}

} The highlighted code is useless and out of date. It says so in VS. tried what it said GetComponent etc, but says theres no reference etc. Doing my brain in wasting all my time trying to figure out faults. Just want things to work. Otherwise i’ll never remember this stuff because the codes are out of date, and i’m supposed to forget them but tutorials keep bringing this memory twisting/delaying junk up.

There’s a solution, no need to get too worked up. Please remember to use code tags, also.

public class PlayerController : MonoBehaviour
{
public float speed;
RigidBody rb;
void Awake () {
   rb = GetComponent<RigidBody>();
 }
private void FixedUpdate() //for physics...
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
   rb.velocity = movement * speed;
}
}

There you go. To be clear, you could have used:

GetComponent<RigidBody>().velocity = movement * speed;

but it’s better to cache a frequently used component.

honestly, i don’t know what you mean by code TAGS, or cache the component :slight_smile: and the emoticons are a little limited for me to get a correct expresssion etc,

Don’t panic.

rigidbody is outdated. It used to mean GetComponent(). The problem with that is that GetComponent has a cost, and just rigidbody could imply that it wasn’t doing a GetComponent and was doing some sort of super optimised magic thing, but it wasn’t.

So the good folks at Unity said, no more rigidbody! So everyone has to type GetComponent() and at the same time, everyone is super clear that they are making a GetComponent call. It’s one of those cases where clarity>convenience. It was the right call.

Caching: so instead of doing that somewhat costly GetComponent call every frame (or every FixedUpdate in your code, which generally is almost every frame), you do it once, say in Awake() or in Start() and then instead of having to tell Unity to look for a component (GetComponent) called RigidBody every time, you just tell it, “oh just use that component you looked for already in Start/Awake”, which is obviously faster.

Tags: in order to make code you copy paste on the forums more readable use [ CODE] and [ /CODE] tags (without that extra space), so they are nicely formatted and more readable on the forums.

1 Like

The main reason I was concerned about variables, is because even though I am only creating a basic character controller, on the side etc, I would like to add:
Jump, jump boost, slam, dash, wall hold or magnetise etc, speed boost like sonic, fly on pickup, or new level area etc, wall bounce-jump, break, strafe/slide break, tilt maximum angle definition before falling over sideways etc ( cubes topple sideways after a sharp turn, but increase a sticky grab until too much force/like a car tilt etc).

This is why variables may become a bit maddening.

Leaving old properties like rigidbody around is purely for the sake of the script upgrader. As such it will continue to confuse new users.

I think the script upgrader can be made smart enough to look for a definition for rigidbody, and if there is one, it should not attempt to upgrade. Why wouldn’t this work?

Uhhh, welcome to programming?

I don’t know. On one hand, potentially yeah, it could work, on the other I can see how it can potentially either become less reliable or require too much work.

So instead of that, I think they should just completely remove rigidbody (and other legacy stuff like it), it’s been deprecated for years. Maybe for the really old legacy stuff they could have an upgrader on the asset store so that we don’t need to have them in base unity?

And regardless, it’s not that much of a pain to replace some of these these things manually (and googling the info is pretty easy). Plus anyone upgrading a really old object knows they should do it gradually (like upgrade it to a couple of intermediate versions before upgrading it to the latest one).

Honestly, i’d like to find the delete alerts option in my well, alerts, as I have loads. Searched unity, but it says game alerts :slight_smile: typical…

You mean forum alerts? Why? (Hover over alerts and click alert preferences)

Code tags explained, if you didn’t get it, yet: Using code tags properly - Unity Engine - Unity Discussions

2 Likes

I did that, but there is no delete option for old alerts, that i’m still connected to the page with. I have all the alerts for this page rather than just the last one etc… It doesn’t show an option for delete all except last etc.

Otherwise I either have to look for the thread remembering the name, or have all alerts listed, unless i’m missing/getting confused with an option, probably :slight_smile: