- Home /
character is moving forward on its own
hi everyone, I'm quite new to Unity. So I hope someone can help me. I'm writing a movement script. Moving and jumping is okay but the character will move forward itself. Here's my script: public class NewMovement : MonoBehaviour {
public float speed = 6F;
public float sprintSpeed = 10F;
public float jumpSpeed = 8F;
public float gravity = 20F;
private Vector3 moveDirection = Vector3.zero;
CharacterController cc;
Animator anim;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController> ();
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (cc.isGrounded)
{
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
if (Input.GetKey(KeyCode.LeftShift))
{
moveDirection *= sprintSpeed;
}
Debug.Log("grounded");
}
moveDirection.y -= gravity * Time.deltaTime;
cc.Move (moveDirection * Time.deltaTime);
anim.SetFloat ("Speed", moveDirection.magnitude);
}
void FixedUpdate () {
}
} I really dun know what the problem is. So plz help!
Answer by LightSource · Apr 04, 2015 at 05:43 PM
cc.Move() is in update, which means that it will be called every frame. This will cause it to move continually. If you want to limit movement by key press, put it in an if statement.
See:
It doesn't work. $$anonymous$$aybe I'm doing it wrong. I tried to limit it by WASD, but the character still moves itself. void Update () {
if (cc.isGrounded)
{
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift))
{
moveDirection *= sprintSpeed;
}
Debug.Log("grounded");
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)||Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)||Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)||Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
cc.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
}
-something like that. $$anonymous$$aybe I should place it under somewhere else, other than update? Anyway thx for your help :D
I'm not completely sure what your problem is. Try manually limiting the condition with a while loop, e.g. while(publicBoolean), and see what happens.
Sorry I can't give you better advice here, I have never used the method myself. As a temporary solution you could always use the static variables in Vector3 (http://docs.unity3d.com/ScriptReference/Vector3.html).
ok i found out the problem. It's about the animator. I dun know why it makes the character moves. After I remove it the movement works perfectly. Anyway thx for your help!! :D
Huh, okay then. I'm happy to have assisted you in the direction of the answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613192131im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
JavaScript not working 0 Answers
Rigidbody collision movement similar to a wall 0 Answers
How to rotate character respect ScreenView 0 Answers
Error CS0029 1 Answer
How do i prevent my Player from sliding when i stop walking? 1 Answer