Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by gaberg21 · Nov 27, 2016 at 03:26 PM · c#animationmovementvisual studiovisualstudio

My animation is working but the character won't move.

Why won't my character move?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float moveSpeed;
 
     private Animator anim;
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f ) 
         {
             transform.Translate    (new Vector3 (Input.GetAxisRaw ("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
         }
 
         if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f ) 
         {
             transform.Translate    (new Vector3 (0f, Input.GetAxisRaw ("Jump") * moveSpeed * Time.deltaTime, 0f));
         }
 
         anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
         anim.SetFloat("MoveUp", Input.GetAxisRaw("Jump"));
 
 
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by VindicareTheKiller · Aug 11, 2020 at 10:17 AM

Try This Code but make shure you Character has a 2DRigidbody and a 2DBoxcollider and watch outthat the script has the name "Character2DController "

also check out my Youtube there i make Tutorials too name:"MasterGreenStudios"

i hope it will help you ;))

using UnityEngine;

public class Character2DController : MonoBehaviour { public float MovementSpeed = 1; public float JumpForce = 1; public Animator animator; private bool facingRight;

 private Rigidbody2D _rigidbody;
 void Start()
 {
     facingRight = true;
     _rigidbody = GetComponent<Rigidbody2D>();
 }
         
 void Update()

 {
     if (Input.GetAxis("Horizontal") < 0)
    {
        transform.localScale = new Vector2 (-1.5f, transform.localScale.y);
     }
     if (Input.GetAxis("Horizontal") > 0)
     {
         transform.localScale = new Vector2(1.5f, transform.localScale.y);
     }

     var movement = Input.GetAxis("Horizontal");

     transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;

     animator.SetFloat("Movement Speed", Mathf.Abs(movement));

     {

     }

     if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W) || Input.GetButtonDown("Jump")) && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
     {
         _rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
     }
 }

}

Comment
Add comment · 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
0

Answer by Yoshinator2 · Nov 27, 2016 at 07:21 PM

To me, your logic seems wrong. Instead of having the getAxisRaw inputs in the vector3, you would need to have a RigidBody(Or RigidBody2D if your game is 2D) and modify the velocity from there. To do this, define the RigidBody(Or RigidBody2D) at the top as a variable. This would look something like:

     private RigidBody myRigidBody;

or

     private RigidBody2D myRigidBody;

Then, you would need to get the component in the start function. This would look like:

     myRigidBody = GetComponent<RigidBody>();

or

     myRigidBody = GetComponent<RigidBody2D>();

From there, you would need to completely rephrase your code in your if statements, as there is a much simpler and cleaner way to move a player.

To me, your transform.Translate and all of the code after it make no sense. Instead, you can just change the velocity of the player or object through the rigid body. This is done by calling your rigid body variable's velocity, and creating a new vector 3, with only the X velocity being modified. As shown:

      if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f ) 
      {
          myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, myRigidBody.velocity.z);
      }
 

Or, if you have a 2D game:

      if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f ) 
      {
          myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, 0f);
      }

And for the jump, you would do the same. This time, you should have a new variable defined for the jumpSpeed. Do this at the top by adding:

     public float jumpSpeed;

Then define the speed in the inspector. Instead of modifying the x velocity, you would modify the Y velocity. As shown:

      if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f ) 
      {
          myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed, myRigidBody.velocity.z);
      }

Or if its 2D:

      if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f ) 
      {
          myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed, 0f);
      }

In case you're having trouble finding where any of this goes, here is how the end code should look:

  using UnityEngine;
  using System.Collections;
 
  public class PlayerMovement : MonoBehaviour {
  private RigidBody myRigidBody;
  public float moveSpeed;
 
  private Animator anim;
 
  // Use this for initialization
  void Start () {
      anim = GetComponent<Animator>();
      myRigidBody = GetComponent<RigidBody>();
  }
  
  // Update is called once per frame
  void Update () {
   if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f ) 
   {
       myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, myRigidBody.velocity.z);
   }
 
 
   if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f ) 
   {
       myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed, myRigidBody.velocity.z);
   }
 
      anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
      anim.SetFloat("MoveUp", Input.GetAxisRaw("Jump"));
 
 
  }
  }

Or if its a 2D game:

  using UnityEngine;
  using System.Collections;
 
  public class PlayerMovement : MonoBehaviour {
  private RigidBody2D myRigidBody;
  public float moveSpeed;
 
  private Animator anim;
 
  // Use this for initialization
  void Start () {
      anim = GetComponent<Animator>();
      myRigidBody  = GetComponent<RigidBody2D>();
  }
  
  // Update is called once per frame
  void Update () {
   if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f ) 
   {
       myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, 0f);
   }
 
   if (Input.GetAxisRaw ("Jump") > 0.5f || Input.GetAxisRaw ("Jump") < -0.5f ) 
   {
       myRigidBody.velocity = new Vector3(myRigidBody.velocity.x, jumpSpeed, 0f);
   }
 
      anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
      anim.SetFloat("MoveUp", Input.GetAxisRaw("Jump"));
 
 
  }
  }

Then, for the animator variables, just have them send the myRigidBody.velocity.x to the animator, and have it run the walking animation if the velocity is greater than zero. But because you can move to the left as well, which would be a negative velocity, you would need to use the Mathf.Abs function. As shown:

      anim.SetFloat("MoveX", Mathf.Abs(myRigidBody.velocity.x));
      anim.SetFloat("MoveUp", Mathf.Abs(myRigidBody.velocity.y));

This should work beautifully for you! Please accept this answer if it helped, I spent a good 25 minutes typing this for you. If you have any further questions just ask! Thanks for reading

Comment
Add comment · 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

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

303 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animation/Movement Error 1 Answer

Animations get screwed up when I add OnAnimatorMove function 0 Answers

My movement gets blocked, when I activate the animator 1 Answer

How to make orb walking? 0 Answers

Stopping an animation when key is unpressed 0 Answers


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