- Home /
Question by
noma · Apr 09, 2015 at 04:27 AM ·
movementmove an objectbox
How to move only one object, not multiple objects
Hello! I am implementing moving boxes game. But I have a problem. I want to move only one box in box stack. But, when I lifted some box where is below other boxes, I could lift the box with above boxes. I have to solve this problem! Could you help me? Thank you!
Here is my code for managing mouse.
using UnityEngine;
using System.Collections;
//using ShowMovedText;
public class MouseManager : MonoBehaviour {
//MovedText show_moved;
public LineRenderer dragLine;
float dragSpeed = 3f;
Rigidbody2D grabbedObject = null;
//float distance;
void Start() {
}
void Update() {
if( Input.GetMouseButtonDown(0) ) {
// We clicked, but on what?
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
Vector2 dir = Vector2.zero;
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, dir);
//if(hit != null && hit.collider != null) {
if (hit.collider != null) {
// We clicked on SOMETHING that has a collider
if(hit.collider.GetComponent<Rigidbody2D>() != null) {
grabbedObject = hit.collider.GetComponent<Rigidbody2D>();
grabbedObject.gravityScale = 0;
dragLine.enabled = true;
}
}
}
if( Input.GetMouseButtonUp(0) && grabbedObject != null ) {
grabbedObject.gravityScale = 1;
grabbedObject = null;
dragLine.enabled = false;
}
}
void FixedUpdate () {
if(grabbedObject != null) {
// Move the object with the mouse
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
Vector2 dir = mousePos2D - grabbedObject.position;
dir *= dragSpeed;
grabbedObject.velocity = dir;
}
}
void LateUpdate() {
if(grabbedObject != null) {
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
dragLine.SetPosition(0, new Vector3(grabbedObject.position.x, grabbedObject.position.y, -1));
dragLine.SetPosition(1, new Vector3(mousePos2D.x, mousePos2D.y, -1));
}
}
}
Comment