• 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 anyaspid · Jul 04, 2018 at 06:07 PM · script.

Best way to use classes (move in that case)?

Greetings, everywhere!

I'm so much confusing about dozen ways to achieve the same result. And I do moving my player by different ways and can't understand, that way are best?

Are my code ok? How much I'm goes right? How can I check my code, by which principles? Any great books about making decisions while unity coding? And sorry for my language of course.

Today I use some not-mono classes for moving, like this:

 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
 
     public float moveSpeedAccelerator;
 
     Direction playerDirection;
     Moving playerMoving;
     Rigidbody2D playerRigidbody2D;
 
     void Start()
     {
         playerRigidbody2D = GetComponent<Rigidbody2D>();
         playerDirection = new Direction(1f, gameObject);
         playerMoving = new Moving(playerRigidbody2D, moveSpeedAccelerator);
     }
     
     // Update is called once per frame
     void Update () {
         float HorizontalMove = Input.GetAxis("Horizontal");
         playerDirection.CurrentDirection = HorizontalMove;
         playerMoving.Move(new Vector2(HorizontalMove, 0f), playerRigidbody2D);
     }
 }





 using UnityEngine;
 
 public class Direction {
     int currentDirection;
 
     GameObject affectedGameObject;
 
     public float CurrentDirection
     {
         get { return currentDirection; }
         set
         {
             if (value != 0 && Mathf.Sign(value) != currentDirection)
             {
                 currentDirection = (int)Mathf.Sign(value);
                 Flip(affectedGameObject);
             }
         }
     }
 
     public Direction(float direction, GameObject _affectedGameObject)
     {
         currentDirection = (int)Mathf.Sign(direction);
         affectedGameObject = _affectedGameObject;
     }
 
     void Flip(GameObject _affectedGameObject)
     {
         Vector3 _tempScale = _affectedGameObject.transform.localScale;
         _tempScale.x *= -1;
         _affectedGameObject.transform.localScale = _tempScale;
     }
 }





 using UnityEngine;
 
 public class Moving {
 
     GameObject movingGameObject;
     Rigidbody2D movingRigidbody2D;
 
     float accelerate;
 
     public Moving(GameObject _movingGameObject, float _accelerate)
     {
         movingGameObject = _movingGameObject;
         accelerate = _accelerate;
     }
 
     public Moving(Rigidbody2D _movingRigidbody2D, float _accelerate)
     {
         movingRigidbody2D = _movingRigidbody2D;
         accelerate = _accelerate;
     }
 
     public void Move(Vector3 _moveDirection, GameObject _movingGameObject)
     {
         _movingGameObject.transform.position += _moveDirection * accelerate;
     }
     public void Move(Vector2 _moveDirection, Rigidbody2D _movingRigidbody2D)
     {
         _movingRigidbody2D.AddForce(_moveDirection * accelerate);
     }
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

152 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

Related Questions

Help a tired man with #C..... (Record Player in Horror Game) 1 Answer

Crafting script Help 0 Answers

Adding scripts to Unity 1 Answer

Particles moving past gun. Please help. 1 Answer

script help 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