I’d like to handle my input in this fashion, and I haven’t seen any solution which works for a boolean array after googling for what seems like forever. All of my input works fine, except when combined with shift and control. Here is some of the code (some brackets may be out of whack, due to the copy/pasting/simplifying):
Have you tried it with RightControl/RightShift? Is it possible your keyboard is sending the wrong signal for left/right control? I believe I’ve seen keyboards before that have sent all shifts as the same button (left or right).
As a side note… that input system looks like it’s a nightmare to write code for. Though it’d be helpful to see how these values are accessed, I can almost certainly say that you’re better off using a Dictionary rather than an array.
Thanks for the reply. I tried right shift, and no luck. I think my keyboard is fine though. It’s actually rather convenient, because RB_Move() and Move_Anim() have the appropriate indices to match Move_Input(). Here are the two functions:
However, now I have a new problem – if shift is held down when “w” is released, the movement stops, but the animation continues. I think the && operator by itself is insufficient here. Anyone have a suggestion?
Well, a dictionary would be an improvement, but seeing the way this is being used, we can go simpler and use the classic “how nearly every Unity character is controlled” pattern:
void FixedUpdate() {
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical") ); // horizontal and vertical default to use WASD
Vector3 movementDirection = Player.transform.TransformDirection(input);
if (movementDirection.sqrMagnitude > 1f) movementDirection.Normalize(); //takes care of diagonals moving too fast
float moveSpeed = 1f;
if (Input.GetKey(KeyCode.LeftShift) ) moveSpeed = 2f;
player_velocity = moveSpeed * movementDirection;
Player.MovePosition(Player.transform.position + (player_velocity * Time.fixedDeltaTime));
}
I’ll admit everything is much simpler since I implemented your code, but now I have a delay on my movement animations, and cpu usage is up. I didn’t change anything else when I changed it. Any ideas why this might be?
The “delay” on the movement is likely due to the input damping that Unity applies; you can try GetAxisRaw instead of GetAxis and see if you like the feel of that better.
As for CPU usage being up, can you elaborate? If it’s just a tiny bit up, that could be explained by having more logic/math in FixedUpdate, but I wouldn’t think it’d be noticeable. Still, you can try moving the first lines of that function (everything up until the last line, really) to Update() instead of FixedUpdate, which may help slightly.
Thanks! GetAxisRaw works waaayyy better in my situation. This is really so much simpler. I’ll look into using native input methods for the bow I implemented too. The booleans and coroutines are crazy there with drawing the bow and animations and whatnot. By the way, the CPU usage is only up a couple percent. It’s worth the switch to save me some headaches. Although, I’m a little surprised it’s even noticeable.
Have you tried profiling it yet to determine the actual cause of the rise of CPU activity? (I’ll be honest, I’ve never profiled the Input functions, I honestly have no idea how efficient they are relative to each other)
I was just looking at the msi afterburner output on my g13. I have already overwritten the old script, so I can’t compare. I didn’t save it since it was so messy. I’m not really worried about it. Thanks again for your help! I could have gone my whole life swimming in a sea of booleans.
OK, one more thing and I’ll quit bugging – I promise. The new input broke my jump script. I have somehow broken the law of inertia. I get crazy different vectors when I print the force vector. Here is the physics portion of the jump script:
Dude should have a crazy high vertical with the force I’m applying (25,000 N). And, he should jump reaallly far. What am I doing wrong? Do vectors not add like normal in c# ?!
I find it easier to work with setting .velocity rather than using .AddForce - AddForce has too many variables to control for, where velocity is an easily understood “units per second” value and that’s it. So maybe try that?
It does appear that your jump is going to be like 99% horizontal (e.g. movementDirection) - I don’t know what your jump should look like but that doesn’t seem right.
If the other part of your movement is controlling horizontal movement, you may not need a horizontal part of your jump at all?
I only added horizontal because he wasn’t moving horizontally. I realize it’s an extremely large jump force in horizontal direction, but player is essentially stopping in horizontal direction while barely jumping vertically. I’m 99% sure the physics above is correct (yet ridiculously huge), and it has something to do with my coding. He should jump 25 meters vertically, while jumping super far. My player is scaled to be about 6 feet and 100 kilos. When I jump in place, it works as intended. The problem is only while moving. I’ll try adding an initial velocity instead.