#Help with player movement

1 messages · Page 1 of 1 (latest)

wispy ivy
#
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField]
    private float speed = 10f;

    private Transform player1Transform;
    private Transform player2Transform;

    private Vector3 movementVect3plr1 = Vector3.zero;
    private Vector3 movementVect3plr2 = Vector3.zero;

    private void Awake()
    {
        player1Transform = GameObject.Find("Player1").GetComponent<Transform>();
        player2Transform = GameObject.Find("Player2").GetComponent<Transform>();
    }

    private void FixedUpdate()
    {

        if (Input.GetKey(KeyCode.W))
        {
            movementVect3plr1.y = 1f;
        }
        if (Input.GetKey(KeyCode.S))
        {
            movementVect3plr1.y = -1f;
        }

        if (Input.GetKey(KeyCode.UpArrow))
        {
            movementVect3plr2.y = 1f;
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            movementVect3plr2.y = -1f;
        }

        player1Transform.position += speed * Time.deltaTime * movementVect3plr1;
        player2Transform.position += speed * Time.deltaTime * movementVect3plr2;
    }
}

I use this script to handle player input but for some reason now when I press the button only once the player moves all the way up or all the way down without stopping. It used to work before, then suddenly stopped working as intended.

verbal cloak
wispy ivy
#

why did it work before then

verbal cloak
#

I would not use ```cs
Input.GetKey(KeyCode.W)

#

idk, did you change the code in any ways

wispy ivy
#

i can't use Input.GetAxisRaw

verbal cloak
#

Why not?

wispy ivy
#

why would I

verbal cloak
#

why would you use GetAxisRaw in the first place

verbal cloak
# wispy ivy why would I

Because that returns -1 to 1 based on WASD keypress (or arrowkeys for that matter), and it the most convinient build in for this

wispy ivy
#

to change the "y" of movementVect3

verbal cloak
#

You can still achieve that with GetAxis xD

#

GetAxis(Vertical) returns -1 to 1 by pressing W or S

wispy ivy
verbal cloak
#

Let's first go over the reason it doesn't work, which is because the input value is never 0. It's always going to be either 1 or -1. That value needs to be 0 whenever you don't press down any keys

wispy ivy
#

oh shoot

#

i know why it doesn't work

#

i declared the Vector3s in the wrong scope

verbal cloak
#

Ok please do explain

wispy ivy
#

well I fixed it

#

I declared the two vectors inside the Update method

#

I probably moved them in the class scope accidentally

#

so they were only set once at init time

verbal cloak
#

So you intended to reset the vector each update

wispy ivy
#

yeah

#

to vector3.zero

verbal cloak
#

ok, sure. But this can be optimized, as that code is quite shit tbh. But if you don't care anymore(just want it to work) then I won't care to explaion either, it's all up to you

wispy ivy
#

it's not meant to be optimized

#

it's just a random prototype for my main project

verbal cloak
#

Which is fair ^^ but it's always a good idea to develop good habits over bad ones even if it's "just a prototype".

wispy ivy
#

What's wrong with it anyways

#

The only thing that's bad is GameObject.Find which isn't always reliable when working on bigger projects

verbal cloak
#

Setting the vector to zero every frame is redundant. Having inputs in the FixedUpdate is NEVER a good idea. Not using GetAxis. Also for your code, you don't even need a Vector3 if you only plan on moving in one axis, those vectors could have been a float

#

You can also customize the GetAxis("YourCustomizedOne"). To return a value based on your own customized keys

wispy ivy
#

fixedupdate updates more frequently though

verbal cloak
#

The difference is that Update gets called every single frame. While FixedUpdate is, as the name suggest, called in a Fixed time. Which by default is 50 times per second (iirc). This means that input in the Update is Guaranteed to work, while it's not in FixedUpdate.

#

Now this means if you run on 120 fps, the Update gets called more frequently then FixedUpdate, a 120 times per second vs 50. So it highly depends on your computer

#

You could ofc set the FixedUpdate to call more frequently, but the point is that when a key is pressed, it's not always guaranteed to be noticed as you might not click it at the exact same frame as the FixedUpdate got called

wispy ivy
verbal cloak
#

Also, FixedUpdate is used for physics, and since you are not using rigidbody it's redundant to use it in the first place

wispy ivy
#

you can use it for anything but it just updates more frequently than Update()

verbal cloak
wispy ivy
#

(which of course means the actions you execute impact more the performance)

verbal cloak
wispy ivy
#

yeah

#

that's what I'm saying

verbal cloak
# wispy ivy that's what I'm saying

Then I've missed that. And still can't see where you've pointed out that FixedUpdate is used for Physics Calculations. Which doesn't seem to make sense to me as you're not even using Unity's physics engine at this point

#

Except for the very use of that function I guess.

wispy ivy
verbal cloak
#

Then you've missed my point. FixedUpdate was made for Physics, and should only be used for that. I merely said that sure, you don't have to, but that's what it's made for

#

You don't nail down nails with a screwdriver, you use a hammer. Use the tools as they should be used. In this case, inputs should be put in Update, But by all means put all your code in FixedUpdatekekdog

wispy ivy
#

alright

#

i'll use Update()

#

sorry if I seemed arrogant

verbal cloak
#

I'm literally a donkey though, I can be very arrogant too. But remember that I'm only here to help you, but its up to you to take in the advice or not. Btw use the documentation alot, you'll find most answers in there

wispy ivy
#

Alright thanks