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 /
avatar image
0
Question by invo1 · Jul 10, 2017 at 09:59 PM · rigidbody2dphysics2d

problem with physics2D

i want to make a game where the player moves forward and you must control terrain by dragging platforms.problem is when i move platform up it adds a force to the player and i don't know how to cancel it.Both player and platform are dynamic because i want platforms to go between 2 points where are box colliders. Code for moving Platform:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveObject : MonoBehaviour {
 
     private float distance = 10f;//for Android raycast
     public bool horizontal;//is moving on X axis?
     public bool vertical;//is moving on Y axis?
 
     private Vector3 screenPos;//screen position
     private Vector3 offset;//offset of the drag
     private Vector3 currentScreenPos;//current screen position
     private Vector3 currentPos;//current player position
 
     Vector3 objPosition;
     Vector3 mousePosition;
 
     private Rigidbody2D rb2D;
     bool moveWithTransform;
 
     private void Start()
     {
         //set instances
         screenPos = Vector3.zero;
         currentScreenPos = Vector3.zero;
         offset = Vector3.zero;
         currentPos = Vector3.zero;
         rb2D = transform.GetComponent<Rigidbody2D>();
         objPosition = transform.position;
     }
     //there i make the function for dragging the terrain
     private void OnMouseDrag()
     {
         mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
         objPosition = Camera.main.ScreenToWorldPoint(mousePosition);      
     }
     //parrenting for object not going through collider on vertical up moving
     private void OnCollisionEnter2D(Collision2D collision)
     {
         
         if (collision.gameObject.CompareTag("Player"))
         {
             if (vertical && !horizontal)
             {
                 collision.transform.parent = transform;
             }
         }
     }
     //unparent on exit
     private void OnCollisionExit2D(Collision2D collision)
     {
         if (collision.gameObject.CompareTag("Player"))
         {
             if (vertical && !horizontal)
             {
                 collision.transform.parent = null;
                 collision.transform.localScale = new Vector3(1f,1f,1f);            
             }
         }
     }
 
     private void Update()
     {
         //for android moving
         if (Input.touchCount > 0f)
         {
             Touch f0 = Input.GetTouch(0);
             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 100))
             {
                 //Check to see if object hit is been set to moveable
                 if (hit.collider.tag == "Moveable")
                 {
                     if (f0.phase == TouchPhase.Began)
                     {
                         screenPos = Camera.main.WorldToScreenPoint(transform.position);
                         offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(f0.position.x, f0.position.y, screenPos.z));
                     }
                     else if (f0.phase == TouchPhase.Moved)
                     {
                         currentScreenPos = new Vector3(f0.position.x, f0.position.y, screenPos.z);
                         currentPos = Camera.main.ScreenToWorldPoint(currentScreenPos) + offset;
                         transform.position = currentPos;
                     }
                 }
             }
         }
     }
     private void FixedUpdate()
     {
         if (horizontal && !vertical)
         {
             rb2D.MovePosition(new Vector2(objPosition.x, rb2D.position.y));
         }
         else if (!horizontal && vertical)
         {
             rb2D.MovePosition(new Vector2(rb2D.position.x, objPosition.y));
         }
     }
 }
 
 Code for moving player:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveObject : MonoBehaviour {
 
     private float distance = 10f;//for Android raycast
     public bool horizontal;//is moving on X axis?
     public bool vertical;//is moving on Y axis?
 
     private Vector3 screenPos;//screen position
     private Vector3 offset;//offset of the drag
     private Vector3 currentScreenPos;//current screen position
     private Vector3 currentPos;//current player position
 
     Vector3 objPosition;
     Vector3 mousePosition;
 
     private Rigidbody2D rb2D;
     bool moveWithTransform;
 
     private void Start()
     {
         //set instances
         screenPos = Vector3.zero;
         currentScreenPos = Vector3.zero;
         offset = Vector3.zero;
         currentPos = Vector3.zero;
         rb2D = transform.GetComponent<Rigidbody2D>();
         objPosition = transform.position;
     }
     //there i make the function for dragging the terrain
     private void OnMouseDrag()
     {
         mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
         objPosition = Camera.main.ScreenToWorldPoint(mousePosition);      
     }
     //parrenting for object not going through collider on vertical up moving
     private void OnCollisionEnter2D(Collision2D collision)
     {
         
         if (collision.gameObject.CompareTag("Player"))
         {
             if (vertical && !horizontal)
             {
                 collision.transform.parent = transform;
             }
         }
     }
     //unparent on exit
     private void OnCollisionExit2D(Collision2D collision)
     {
         if (collision.gameObject.CompareTag("Player"))
         {
             if (vertical && !horizontal)
             {
                 collision.transform.parent = null;
                 collision.transform.localScale = new Vector3(1f,1f,1f);            
             }
         }
     }
 
     private void Update()
     {
         //for android moving
         if (Input.touchCount > 0f)
         {
             Touch f0 = Input.GetTouch(0);
             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 100))
             {
                 //Check to see if object hit is been set to moveable
                 if (hit.collider.tag == "Moveable")
                 {
                     if (f0.phase == TouchPhase.Began)
                     {
                         screenPos = Camera.main.WorldToScreenPoint(transform.position);
                         offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(f0.position.x, f0.position.y, screenPos.z));
                     }
                     else if (f0.phase == TouchPhase.Moved)
                     {
                         currentScreenPos = new Vector3(f0.position.x, f0.position.y, screenPos.z);
                         currentPos = Camera.main.ScreenToWorldPoint(currentScreenPos) + offset;
                         transform.position = currentPos;
                     }
                 }
             }
         }
     }
     private void FixedUpdate()
     {
         if (horizontal && !vertical)
         {
             rb2D.MovePosition(new Vector2(objPosition.x, rb2D.position.y));
         }
         else if (!horizontal && vertical)
         {
             rb2D.MovePosition(new Vector2(rb2D.position.x, objPosition.y));
         }
     }
 }
 ![alt text][1]
 


[1]: /storage/temp/97390-unity.png

unity.png (158.9 kB)
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.

Follow this Question

Answers Answers and Comments

68 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

Related Questions

AddingForceAtPoint not doing anything 0 Answers

How many Kinematic rigidbodies(2D) can i have in a device game without any lag ? 0 Answers

Is it possible for two game objects to reference the same rigid body? 1 Answer

OnTriggerEnter2D is not working 1 Answer

Reset Rigidbody2D rotation after collision 2 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