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 Nova-1504 · Nov 22, 2016 at 09:07 PM · 2dinputspriteflipin-game

How do i get my character to move

All I need to know is how to make my sprite actually move when I push buttons in-build and flip when I turn around. PLEASE 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

3 Replies

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

Answer by Ninjapro2k_ · Nov 25, 2016 at 08:34 PM

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 //You'll need all these bools and floats
 [HideInInspector]
 public bool facingRight = true;
 [HideInInspector]

 public float moveForce = 365;
 public float maxSpeed = 5f;
 public float jumpForce = 1000f;
 public Transform groundCheck;
 float groundRadius = .2f;
 public LayerMask whatIsGround;

 private bool grounded = false;
 private Rigidbody2D rb2d;

 // When the scene starts, grab the RB2d component
 void Awake()
 {
     rb2d = GetComponent<Rigidbody2D>();
 }



 // Update is called once per frame
 void Update()
 {

    //Basically a small area that checks for ground under the player
    grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);


     //If I press jump and i'm touching the ground, jump is true
     if (Input.GetButtonDown("Jump 1") && grounded)
     {
         jump = true;
     }


 }

 //Standard movement stuff adapted from the official tutorial
 void FixedUpdate()
 {
     float h = Input.GetAxis("Horizontal 1");


     if (h * rb2d.velocity.x < maxSpeed)
         rb2d.AddForce(Vector2.right * h * moveForce);

     if (Mathf.Abs(rb2d.velocity.x) > maxSpeed)
         rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);

      //If i'm moving right but not facing right, flip me
      if (h > 0 && !facingRight)
          Flip();
      //If i'm moving left but facing right, flip me
      else if (h < 0 && facingRight)
          Flip(); 


     //If jump is true, push me forward 0 degrees, and upward the value of jumpForce
     if (jump)
     {
         rb2d.AddForce(new Vector2(0f, jumpForce));
         jump = false;
     }


 }

     //facing right = not facing right; theScale = my local scale in the inspector; the x value of theScale = the negative of it's current value; my local scale in the inspector now equals the negative of it's current value (e.g. 30 now equals -30)
     void Flip()
     {
         facingRight = !facingRight;
         Vector2 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     } 
 }

For this you just need a player, with an empty gameObject called as a child. That child should be placed just at the player's feet. Of course attach Rigidbody2D to player. Hope this helps somebody :)

Comment
Add comment · Show 1 · 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 Ninjapro2k_ · Nov 25, 2016 at 08:37 PM 0
Share

Oh, and for the layermask: basically this makes it so anything marked true will be seen as ground.

In the inspector, whatever you don't want to be labeled as ground (e.g. players, walls, projectiles), uncheck them in the layermask. Ground Check should not be marked as ground.

avatar image
1

Answer by tanoshimi · Nov 22, 2016 at 09:09 PM

https://unity3d.com/learn/tutorials/topics/2d-game-creation/2d-character-controllers?playlist=17093

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 Midane53 · Nov 23, 2016 at 07:32 AM

I don't get how questions like this are approved by moderation and get shown up in the main questions " wall ". :V

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

107 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

Related Questions

Sprite 2D Flip issue 1 Answer

Unity2D animation and sprite layout 0 Answers

Applying one sprite/texture across multiple gameobjects. (2D) 0 Answers

Fliping a sprite 1 Answer

Inverse Sprite Maskin 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