Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JoshMBeyer · Sep 02, 2014 at 11:52 PM · c#physics2dplatformerplatform

Need help with 2D Platform somewhat Physics~

I have a platform that interpolates between two points. The issue is that if the player stands still on the moving tile. If he doesn't run with it, it will move out from under his feet. It should be as if in real life. If you jump on something spining, you will spin with it, and so on. This object doesn't spin however, it moves left, right, up, down, and at an angel. Any advice here? This is my current script.

 public class XY_MoveScript : MonoBehaviour
 {
   
     //Movement Parameters.
     public Transform pointA;
     public Transform pointB;
 
     public bool startAutomaticly;
     public bool startFromTrigger;
     
     public GameObject triggerObject;
     public Vector3 offset;
 
 
     public float speed;
     public float delayStartBy = 0;
     private bool go;
     private Transform destination;
    
     private bool yes;
 
 
 
     void Awake() { transform.position = pointA.position; }
    
     void Start()
     {
         //---------------Through Errors if Needed----------------
         if (!startFromTrigger && !startAutomaticly) { Debug.LogError("You have not specified to StartAutomaticly or StartFromTrigger!" + this.gameObject); return; }
         if (startAutomaticly && startFromTrigger) { Debug.LogError("You have StartFromTrigger And Start automaticly checked on " + this.gameObject + "!"); return; }
         if (startFromTrigger && !triggerObject) { Debug.LogError("You have Start from trigger checked, but you forgot to apply the TriggerGO1 prefab!" + this.gameObject); return; }
         //-------------------------------------------------------
 
 
         destination = pointB;
         if (startAutomaticly)
         {
             StartCoroutine(WaitTime());
         }
         else if (startFromTrigger)
         {
             go= false;
             GameObject trigger = Instantiate(triggerObject, transform.position + offset, transform.rotation) as GameObject;
         }
     }
 
     void Update()
     {
         if (go)
         {
             float step = speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, destination.position, step);
 
             if (transform.position == pointB.position)
             {
                 destination = pointA;
             }
             else if (transform.position == pointA.position)
             {
                 destination = pointB;
             }
         }
 
     }
     private IEnumerator WaitTime()
     {
         //Wait this long.
         yield return new WaitForSeconds(delayStartBy);
         go = true;
     }
 
 }
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
1
Best Answer

Answer by JaneHafelman · Sep 03, 2014 at 02:29 PM

It is a very bad code style.

You should not use boolean variables. You should split your logic into STATES that can be done with enums like EState{ Jump, Landed, Fire } and make some swith logic like

if ( State == Jump ) { //Jump logic } else if ( State == Landed ) { //Landed logic }

That much easier to find bugs and read the code. Then we can read your code and give normal answer

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 JoshMBeyer · Sep 11, 2014 at 04:11 AM 0
Share

Can you give me a link or what to search to learn more about states, how to define them, and so on. Because that does sound alot better, and I would like to improve in my coding efficiency.

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Move Physics2D Object With Mouse Using AddForce? 0 Answers

Is there a convenient way to check if two gameobjects are touching BUT NOT overlapping? 2 Answers

Is there a way to lock velocity? 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