- Home /
 
               Question by 
               ctulhu23 · Nov 04, 2015 at 12:14 PM · 
                planemovingcoordinatesrepeating  
              
 
              Moving object on plane
Good day!
Please help with locked moving object on plane. Exist object A on plane B. He moving repeatly from one side of palne to another. I make mathematical calculations to get left and right cordinates of plane B and object A. If plane B scale more than 3 - begin troubles. Object A just moving to the end of plane and fall. In debug log i can see than coords of object A less than plane B.
Here what i mean: 
Here code: using UnityEngine; using System.Collections;
 public class Enemy : MonoBehaviour {
     private float PlaneSizeX = 64;
     private float EnemySizeX = 64;
     private float eLeft; //enemy left position
 
     private float pLeft; //plane left position
     private float pRight; //plane right position
     private float Speed = 1;
     private Rigidbody2D mBody;
     private bool grounded = false;
 
     // Use this for initialization
     void Start () {
         mBody = GetComponent<Rigidbody2D>();
         eLeft = EnemySizeX * mBody.transform.localScale.x;
         eLeft = -(eLeft / 2) + (mBody.transform.position.x * EnemySizeX / 2);
         Debug.Log ("Enemy x : " + eLeft + " " + (eLeft + EnemySizeX * mBody.transform.localScale.x));
     }
     
     // Update is called once per frame
     void Update () {
         eLeft = mBody.transform.position.x * EnemySizeX - (EnemySizeX / 2);
         //eRight = mBody.transform.position.x * EnemySizeX + (EnemySizeX / 2);
         if (grounded) {
             //right test
             if ((eLeft + EnemySizeX * mBody.transform.localScale.x) >= pRight) {
                 Debug.Log ((eLeft + EnemySizeX * mBody.transform.localScale.x) + " >= " + pRight);
                 Speed = 0;
             }
             //left test 
             //if (eLeft <= pLeft)  {
             //    Speed = 1;
             //}
         }
         mBody.velocity = new Vector2(Speed, 0);
         Debug.Log (eLeft + " " + (eLeft + EnemySizeX*mBody.transform.localScale.x) + " " + mBody.transform.localScale.x);
     }
 
     void OnCollisionStay2D(Collision2D coll)
     {
         grounded = true;
         Collider2D Plane = coll.gameObject.GetComponent<Collider2D> ();
         //pLeft = (Plane.transform.position.x * PlaneSizeX - (PlaneSizeX / 2)) * Plane.transform.localScale.x;
         //pRight = (Plane.transform.position.x * PlaneSizeX + (PlaneSizeX / 2)) * Plane.transform.localScale.x;
         float ScaledSize = PlaneSizeX * Plane.transform.localScale.x;
         pLeft = -(ScaledSize)/2 + (Plane.transform.position.x * PlaneSizeX/2);
         pRight = (ScaledSize)/2 + (Plane.transform.position.x * PlaneSizeX/2);
         Debug.Log("Plane x: " + pLeft + " " + pRight);
     }
 }
 
                 
                screen.png 
                (17.4 kB) 
               
 
              
               Comment
              
 
               
              Answer by ctulhu23 · Nov 09, 2015 at 05:44 AM
Get helped. Wrong calculations.
 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
     private float eLeft;
     private float eRight;
 
     private float pLeft;
     private float pRight;
     private float Speed = 1;
     private Rigidbody2D mBody;
     private BoxCollider2D mCollider;
     private bool grounded = false;
 
     // Use this for initialization
     void Start () {
         mBody = GetComponent<Rigidbody2D>();
         mCollider = GetComponent<BoxCollider2D>();
         eLeft = transform.position.x - mCollider.bounds.size.x / 2;
         eRight = transform.position.x + mCollider.bounds.size.x / 2;
     }
     
     // Update is called once per frame
     void Update () {
         eLeft = transform.position.x - mCollider.bounds.size.x / 2;
         eRight = transform.position.x + mCollider.bounds.size.x / 2;
         if (grounded) {
             //right test
             if (eRight >= pRight) {
                 Speed = -1;
             }
             //left test 
             if (eLeft <= pLeft)  {
                 Speed = 1;
             }
         }
         mBody.velocity = new Vector2(Speed, 0);
     }
 
     void OnCollisionStay2D(Collision2D coll)
     {
         grounded = true;
         Collider2D Plane = coll.gameObject.GetComponent<Collider2D> ();
         pLeft = Plane.transform.position.x - Plane.bounds.size.x / 2;
         pRight = Plane.transform.position.x + Plane.bounds.size.x / 2;
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Get the borders of screen 1 Answer
Get mouse positon in world coordinates on a plane? 2 Answers
Raycast visual and moving text possition. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                