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.
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);
}
}
I don’t know if that’s exactly the problem, but if you’re using
FixedUpdate
you don’t need consider the movement byTime.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 onlyUpdate
or the multiplication of the value ofTime.deltaTime
and see if any of these options work.– Luiz Vieira
The
FixedUpdate
updates every 0.02 seconds, thedeltaTime
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?– Nils
Also does not turn towards the mouse guy
– pedrofarah
thanks for the tip Luiz but did not give :(
– pedrofarah
@Nils That may not be the problem, but the
deltaTime
certainly affects the movement planned, No? For example, in his code the "speed" defined is6f
, and how it multiplies bydeltaTime
(which, as you say yourself, is fixed at0.02
on calls fromFixedUpdate
) the value effective of speed will be0.02 * 6 = 0.12
. Thus, at each frame, the character will be moving only 0.12 of the unit vector of Unity.– Luiz Vieira
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)?– Luiz Vieira
Try it like this:
playerRigidbody.MovePosition(transform.position + transform.forward * speed);
. Also try to put some debug messages (withDebug.Log
) to see which values are being calculated/used in the methodMove
.– Luiz Vieira
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
– pedrofarah
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.– Luiz Vieira
Finally it worked out man...simple lack of attention my ...thank you so much!!
– pedrofarah
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?
– pedrofarah
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.
– Luiz Vieira
Thanks brother...the first problem is already solved. Qto to the second, I will open a new question
– pedrofarah
That question is being discussed at the goal. Especially those who voted to close as duplicate, are invited to participate in the discussion. :)
– Luiz Vieira