- Home /
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
Your answer