Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Travox · Jul 10, 2015 at 10:30 AM · 2dmoveflip

Applying Force in 2D and calling a function

Okay so i watched the 2d platformer tutorial and followed along then redid it without the video to get used to what to do now im trying to set it up myself from scratch.

Ive hit a snag with the C# I want to use set keys instead of input axis with the current setup(see below) after going through the flip function over and over trying it throughtout the code in segments i cannot get it to apply without calling it as a function but there is my second snag i thought dropping the addforce and flip into a function together would allow me to do both on keypress whereas the current way only flips and if flip isnt there it adds the force. Can anyone point me towards why?

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public Rigidbody2D rg2d;
 public float movespeed = 10f;
 public bool facingRight = true;

 void Start () {    
 }
 
 void FixedUpdate () {
     rg2d = GetComponent <Rigidbody2D> ();
     if ((Input.GetKey (KeyCode.D)) && !facingRight)
         moveright ();
     if ((Input.GetKey (KeyCode.A)) && facingRight)
         moveleft ();
 }
 void Flip(){
     facingRight = !facingRight;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }

 void moveright(){
     rg2d.AddForce (new Vector2 (movespeed, 0f));
     Flip ();
 }

 void moveleft(){
     rg2d.AddForce (new Vector2 (-movespeed, 0f));
     Flip ();
 }
                       }

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 superpentil · Jul 10, 2015 at 11:02 AM

First off, it's considered bad practice with things such as player movement to use KeyCode instead of GetAxis becuase the axis are remapable. You really only want to use KeyCode for things like pressing Escape to bring up pause menus.

You're assigning rg2d wrong. If you want to get components on the current gameobject you use:

 rg2d = gameObject.GetComponent<Rigidbody2d>();

gameObject is different than GameObject. Basically, GameObject is an overarching class. gameObject is the current instance.

The logic in your script is as follows:

  1. If user has pressed 'D' key AND we aren't facing right, then move right.

  2. If user has pressed 'A' key AND we are facing right, then move left.

Doesn't make much sense does it? We want to be able to move in any direction at anytime (I'm assuming that's what you want). From what I recall, you want to do the flip if you're facing the opposite direction to where you plan to go. The logic you want is:

  1. If user has pressed 'D' key, check to see if we're facing right. If we aren't, then flip. Either way we still want to move.

  2. If user has pressed 'A' key, check to see if we're facing left. If we aren't, then flip. Either way we still want to move.

You're better off nesting your if's.

 if(Input.GetKey(KeyCode.D))
 {
     if(facingRight == false)
     {
         Flip();
     }
 
     moveRight();
 }

 if(Input.GetKey(KeyCode.A))
 {
     if(facingRight == true)
     {
         Flip();
     }
 
     moveLeft();
 }

Notice that the !facingRight is gone. !something can mean a couple things. The exclamation point means NOT. In some languages calling !something can return either the opposite value, 0, null, nil, or whatever weird stuff the language does that means 0 or the opposite. Just be explicit and use true or false.

Take Flip() out of moveRight() and moveLeft(). You actually don't even need those functions there but if you plan on doing something else with movement then keep them as functions.

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 superpentil · Jul 10, 2015 at 11:08 AM 0
Share

I also forgot to mention this, don't assign stuff the way you are doing on FixedUpdate(). You should always assign things that you plan on immediately using in Awake() or Start(). It causes performance issues.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I make my Enemy look/flip towards my player (2D TOP-DOWN) 1 Answer

Mouse Click to keyCode 1 Answer

Raycast2D will point right, but won't point left when player turns. 1 Answer

Rotate/Flip Image in Unity 2d using Virtual Joystick 1 Answer

Something wrong with my movement script... 3 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