Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Big2 · Apr 04, 2015 at 02:21 PM · character movement

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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:

http://docs.unity3d.com/ScriptReference/Input.GetKey.html

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Big2 · Apr 05, 2015 at 03:20 PM 0
Share

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

avatar image LightSource · Apr 05, 2015 at 07:01 PM 0
Share

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).

avatar image Big2 · Apr 06, 2015 at 03:29 AM 0
Share

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

avatar image LightSource · Apr 06, 2015 at 03:31 AM 0
Share

Huh, okay then. I'm happy to have assisted you in the direction of the answer.

avatar image _Kyle_1 · Apr 06, 2015 at 04:15 AM 0
Share

Using that code would remove gravity from the player.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges