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
0
Question by Psyonicg · Feb 17, 2016 at 05:47 PM · movementrigidbody2d-platformer

2D Character Rigidbody not moving with arrow keys.

I am creating a movement script which includes jumping and running but while my character animates it will not move despite speed being above 0. Here's the code.

using UnityEngine; using System.Collections;

// TODO - Add in Jumping and animation handling, probably sounds too

public class PlayerMovement : MonoBehaviour { public float speed = 100; // Seems good - subject to change private float initialSpeed = 100; //copies the speed from the player movement speed "5" public float jumpHeight; bool canJump = true;

 //public int playerScore = 0; //public int, this means other scripts can access this
 //public GUIText scoreOutput; // display text in game  - assigned with scoretext game object


 Rigidbody2D RigidBodyHandling;
 Vector2 StopMove;
 Vector3 scale;
 Animator AnimatorHandling;


 // Use this for initialization
 void Start()
 {
     RigidBodyHandling = gameObject.GetComponent<Rigidbody2D>();
     AnimatorHandling = gameObject.GetComponent<Animator>();

 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetAxis("Horizontal") > 0) // moving right
     {
         
         scale = new Vector3(1.851962f, 1.643932f, 1);
         transform.localScale = scale; // Sets player sprite to face right
     }
     else if (Input.GetAxis("Horizontal") < 0) // moving left
     {
         
         scale = new Vector3(-1.851962f, 1.643932f, 1);
         transform.localScale = scale;  // Sets player sprite to face left
     }

     if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Horizontal") != 0)
     { //if player holds down shift, and is pressing a movement direction set the max speed & anim to 1.5
         speed = initialSpeed * 1.5f; // multiplying the current speed by 1.5
         AnimatorHandling.SetFloat("Walk", 1.5f); //speeds up the animation to match
     }
     else {            //If shift is not held down, start walking by with default speed
         speed = initialSpeed; //defaults back to original speed "5"
         AnimatorHandling.SetFloat("Walk", Mathf.Abs(Input.GetAxis("Horizontal"))); //sets the animation back to "5" too
     }


     StopMove = new Vector2(0, RigidBodyHandling.velocity.y);

     if (Input.GetAxis("Horizontal") != 0 && (RigidBodyHandling.velocity.x < speed && RigidBodyHandling.velocity.x > -speed)) //If player is moving in X or -X direction
     {
         RigidBodyHandling.AddForce(Vector2.right * Input.GetAxis("Horizontal") * 10); //Moves the player 
     }
     else if (Input.GetAxis("Horizontal") == 0) // If <-/-> isn't pressed
     {
         RigidBodyHandling.velocity = StopMove; // Stop the player
     }

     if (Input.GetButtonDown("Jump")) //if the player presses jump
     {
         if (canJump == true) //if the player can currently jump
         {
             AnimatorHandling.SetBool("Jump", true); //play the jump animation
             RigidBodyHandling.AddForce(transform.up * jumpHeight); //give the rigid body upward force
             canJump = false; //stops the player jumping while already in the air
         }
     }

     ////take the score output variable and convert it to a string
     //scoreOutput.text = ("Score: " + playerScore.ToString()); // linking back to text - types out score + " playerscore" string - the number
 }

 void OnCollisionEnter2D(Collision2D coll)            //check if the player collides with the ground again.
 {
     if (coll.gameObject.tag == "Ground")
     {
         AnimatorHandling.SetBool("Jump", false);    //stops the jump animation/setting the char back to idle.

         AnimatorHandling.SetBool("Land", true);     //starts the landing animation.
         StartCoroutine(LandingAnimationWait());
     }
 }

 IEnumerator LandingAnimationWait()                  //A delay while the landing animation plays.
 {
     float WaitTimer = 0.5f;                         //sets the wait time. 
     yield return new WaitForSeconds(WaitTimer);
     canJump = true;                                 //once the landing animation is complete allows player to jump again. 
     AnimatorHandling.SetBool("Land", false);        //ends landing animation
 }

}

I'm really confused here. Any 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

0 Replies

· Add your reply
  • Sort: 

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

52 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

Related Questions

Cant get dash attack working 0 Answers

Determine the direction an object is actually moving? 0 Answers

Problem with rigidbody 2 Answers

Rigidbody going through Colliders help! 1 Answer

Help converting a character controller script to rigidbody 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