Increase Unity Movement When Holding Shift

Started by
9 comments, last by Ovicior 8 years, 5 months ago

Okay, I've looked at the code. I agree with those above that it is a mess.

Input controllers should ONLY give commands to components. They should not need to know anything at all about the character system's state or position, if the character has a skateboard (interesting mechanic) or other state of the character.

The input controller should map input into events that get forwarded to other components.

And you REALLY should not call this line every Update(): CharacterController controller = GetComponent<CharacterController>();

And this kind of manipulation belongs in FixedUpdate() which is called at a regular simulator time step, rather than Update() which is called irregularly and whenever the engine feels like it.

And probably more things, but that's enough for right now.

So starting out, you should have the input controller have a public variable with the character that needs controlling.

In your FixedUpdate, your first test should be to see if that targeted character is not null. If it is null, log and return.

Then on your character's code object (not shown in your file) have a set of functions. Looks like you've got these functions that belong in your character's class, NOT in your input controller:

* Jump() -- Test to see if the character is on the ground unless you've got some kind of multi-jump system, makes sure the character's other state allows the action, then triggers the animation or the motion stuff that jumps.

* Skate() -- Tests to see if the character is able to start skating, triggers any animations or whatnot, then changes the character's state to skating. When that state is true, the character's own FixedUpdate() should handle moving forward at whatever speed needs to be applied.

* Walk() -- same as Skate, except the character has a different final speed.

* Stop() -- same as Skate, except the new speed is zero.

* Turn(yaw, roll) -- Not exactly sure how a skateboard character turns with its roll that way you've got it in your code, but you have it there so you'll need to figure it out. Instead, either save this in the character's own private variables for later FixedUpdate handling, or do something creative to make it happen immediately on call. Don't make the character code figure out the input device's axis data.

That change will simplify your logic considerably, an let you move on to solving other problems.

I've tried this a few times. Move a majority of that code around, then have things like Run(); Jump(); etc. in Update();

I also tried to use FixedUpdate, which made it return to its original position every other frame or so.

The code works as it is. I'm not quite sure how to split it. Any guides?

What will you make?

This topic is closed to new replies.

Advertisement