Nightmares character does not move

Asked

Viewed 182 times

2

I’m having a problem in the game Nightmares. My character doesn’t move just makes the animation of Idle.

The script There you go and there seems to be no mistake.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6f;

    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLenght = 100f;

    void Awake ()
    {
        floorMask = LayerMask.GetMask ("Floor");
        anim = GetComponent <Animator> ();
        playerRigidbody = GetComponent <Rigidbody> ();

    }


    void FixedUpdate ()
    {
        float h =Input.GetAxisRaw("Horizontal");
        float v =Input.GetAxisRaw("Vertical");

        Move (h,v);
        Turning ();
        Animating (h, v);

    }


    void Move (float h, float v)
    {

        movement.Set (h, 0f, v);

        movement = movement.normalized * speed * Time.deltaTime ;

        playerRigidbody.MovePosition (transform.position + movement);

    }

    void Turning ()
    {

        Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

        RaycastHit floorHit;

        if(Physics.Raycast (camRay, out floorHit, camRayLenght, floorMask))
        {

            Vector3 playerToMouse = floorHit.point - transform.position;

            playerToMouse.y = 0f;

            Quaternion newRotation = Quaternion.LookRotation (playerToMouse);

            playerRigidbody.MoveRotation (newRotation);
        }




    }


    void Animating (float h,float v)
    {

        bool walking = h != 0f || v !=0f;

        anim.SetBool ("IsWalking", walking);
    }


}
  • 1

    I don’t know if that’s exactly the problem, but if you’re using FixedUpdate you don’t need consider the movement by Time.deltaTime. Perhaps this is greatly slowing down the character’s speed of movement and so the effect is as if he is not moving. Try changing the method name to only Update or the multiplication of the value of Time.deltaTime and see if any of these options work.

  • The FixedUpdate updates every 0.02 seconds, the deltaTime should not affect the movement. I imagine there are some things, the script might not be in the player for example. @pedrofarah, could please add a printscreen of the Player Inspector. The player does not move, or also does not rotate towards the mouse?

  • Also does not turn towards the mouse guy

  • thanks for the tip Luiz but did not give :(

  • 1

    @Nils That may not be the problem, but the deltaTime certainly affects the movement planned, No? For example, in his code the "speed" defined is 6f, and how it multiplies by deltaTime (which, as you say yourself, is fixed at 0.02 on calls from FixedUpdate) the value effective of speed will be 0.02 * 6 = 0.12. Thus, at each frame, the character will be moving only 0.12 of the unit vector of Unity.

  • Peter, another thing I just noticed now is that you’re making the move only based on the axes (keyboard/joystick), which as you observe in the documentation return values in the range [-1, 1] (and in the case of keyboard, only with values -1, 0 or 1). You should also use the transform.forward (to move the character in the direction he’s pointing/looking at)?

  • Try it like this: playerRigidbody.MovePosition(transform.position + transform.forward * speed);. Also try to put some debug messages (with Debug.Log) to see which values are being calculated/used in the method Move.

  • Still no gave Uiz...the strange is that I made the way of the Nils video...agr appeared a problem on the console.. I’ll put it for you to see

  • 1

    Ah. It’s explained then (and @Nils was right). You didn’t add one Rigid body to the object of the player, and so when playerRigidbody.MovePosition (transform.position + movement); nothing happens. I don’t know how the Nils video is, but go back there and pay attention to the part where he adds this component to the player object (see the button add Component). Do it and it should work.

  • Finally it worked out man...simple lack of attention my ...thank you so much!!

  • There’s just one more problem...when he walks, he doesn’t do the "move" animation all the time.. does it once and dps simply "drag".. It’s normal?

  • It’s not normal if what you wanted was the animation running as it moves. But then the problem is in your animation graph, so go back to the Nils video and look again if you didn’t miss out on something else related. If doubt remains, open another question.

  • Thanks brother...the first problem is already solved. Qto to the second, I will open a new question

  • 1

    That question is being discussed at the goal. Especially those who voted to close as duplicate, are invited to participate in the discussion. :)

Show 9 more comments

1 answer

3


Just to ask for an answer (and maybe help other people in the future), I will post here the conclusion of the comments. Note that the merit of the "solution" of the AP problem is also @Nils. :)

On game startup, that is, on the method call Awake, your code does the following:

playerRigidbody = GetComponent <Rigidbody> ();

This code snippet gets the component Rigidbody attached to the current object (of the player) and stores in the variable playerRigidbody.

So during the drive, your code does the following:

playerRigidbody.MovePosition (transform.position + movement);

That call, the method MovePosition class Rigidbody will only work if this variable is not null.

As the error itself indicates (in English), there is no "rigid body" (from English, Rigid body) attached to the player object (in your example, the object Player):

"There is no 'Rigidbody' Attached to the "Player" game Object, but a script is trying to access it."

In free translation:

"There is no 'Rigidbody' component attached to the game object "Player", but a script is trying to access it."

Precisely, because in his call to MovePosition the variable playerRigidbody is void, since you forgot to attach the 'Rigidbody' component to the player object.

  • @Luis Vieira, thanks for the mention, but you who killed the riddle, I had not even thought about it (I thought he forgot to save the script or hang it on the player). And beautiful explanation that you gave here in the answer, I will try to learn this your didactic is very clear, fast and explanatory (beautiful way of saying that I will plagiarize in the face wide your way of explaining).

  • 1

    @Nils If you hadn’t asked for a printscreen of the Inspector you wouldn’t be sure of the problem. : ) You can "plagiarize" without fear! hehe I really think the best way to teach people to be self-sufficient is by showing that error messages are there to help and not to scare.

  • @Nils You who know more of the subject, could please provide an answer in this other question of the same AP regarding why the error occurred when "Has Exit Time" influenced? I thought about asking the AP itself to do this, but I think you have a better chance of producing an answer that will be useful to other people in the future (especially consumers of your videos). I also don’t understand all the nuances of this setting... :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.