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 funzrey · Nov 19, 2015 at 01:14 PM · c#2dsandbox

How do you create a 2D player movement script?

Hello everyone! I am a little new to unity C# coding (I know C# very well though) and I have been creating a 2d sandbox, Right now I have all of my sprites in, but I need to know how to make a player controller, I already have most of the script, but I am stuck at the moving part (commented as //stuck here), Thanks for any help in advance. (p.s. I know Vector3 and Vector2 as I did them in one of my coding classes a while back)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMover : MonoBehaviour {
     //init variables
     public GameObject target = this.GetComponent(); //also an error here.
     public float Speed = 0f;
     public float move = 0f;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         KeyCode RightKey = KeyCode.D; //shortcut
         KeyCode LeftKey = KeyCode.A; //shortcut
         if(Input.GetKey (RightKey)) //when Right Key pressed (D)
         {
             //stuck here
         }
         else if(Input.GetKey (LeftKey)) //when Left Key pressed (A)
         {
             //stuck here
         }
     }
 }
 
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
0

Answer by _Game_Dev_Dude_ · Nov 19, 2015 at 02:26 PM

There are two ways of solving this. Using unity physics or handling the movement calculations yourself and editing the transform. I would suggest using unity's built in 2D physics.

To get you started, you need to add a Rigidbody2D component to your player object. Then in this script where you commented "stuck here", you will use the following :

 GetComponent<Rigidbody2D>().AddForce( movementForce);


Hope this helps!

Comment
Add comment · Show 6 · 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 funzrey · Nov 19, 2015 at 04:14 PM 0
Share

Good job! I will try it out!

avatar image funzrey · Nov 19, 2015 at 04:20 PM 0
Share

@Game_Dev_Dude I get an error of it saying: The type or namespace RigidBody2D could not be found, I even put the rigid body inside the 2d player sprite.

avatar image _Game_Dev_Dude_ funzrey · Nov 19, 2015 at 04:25 PM 0
Share

Sorry typo in my original comment. The 'B' in RigidBody should be lower case.

 GetComponent<Rigidbody2D>().AddForce( movementForce);
avatar image funzrey · Nov 19, 2015 at 04:34 PM 0
Share

No errors, but it dosen't work still

     //snip of the code
         void Update () {
             $$anonymous$$eyCode Right$$anonymous$$ey = $$anonymous$$eyCode.D; //shortcut
             $$anonymous$$eyCode Left$$anonymous$$ey = $$anonymous$$eyCode.A; //shortcut
             if(Input.Get$$anonymous$$ey (Right$$anonymous$$ey)) //when Right $$anonymous$$ey pressed (D)
             {
                 GetComponent<Rigidbody2D>().AddForce(Vector2.right);
             }
             else if(Input.Get$$anonymous$$ey (Left$$anonymous$$ey)) //when Left $$anonymous$$ey pressed (A)
             {
                 GetComponent<Rigidbody2D>().AddForce(Vector2.left);
             }
         }
     }
 
avatar image _Game_Dev_Dude_ funzrey · Nov 20, 2015 at 08:26 AM 0
Share
  • $$anonymous$$ake sure your rigid body does not have the "is$$anonymous$$inematic" checkbox checked in the inspector.

  • $$anonymous$$ake sure the mass on your rigid body is something small like 1.

  • Also those vectors are very small in comparison with the world. Try multiplying them by 100 and see if they still do nothing.

    AddForce(Vector2.right * 100);

avatar image funzrey _Game_Dev_Dude_ · Nov 20, 2015 at 08:39 PM 0
Share

@Game_Dev_Dude Sorry about late reply, Even multiplying by 1000 dosen't work.

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

48 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

Related Questions

Making a list of images within a scrollrect generated from prefabs tappable/clickable 1 Answer

Drag 2D Sprite by Touch 1 Answer

Move GameObject to Position, Waiting, and then Moving to Another Position 5 Answers

Unity jump from one canvas scene to another scene with canvas 2 Answers

C# Script - Erro CS8025: Parsing error 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