Question by
madman_118 · Aug 27, 2015 at 05:23 PM ·
unity 5scripting problemmonodevelopdrag objects
Help me please... I have this script but its doesn't drag objects
using UnityEngine;
using System.Collections;
public class MouseManager : MonoBehaviour {
Rigidbody2D grabbedObject = null;
Vector2 mousePos2D;
float dragSpeed = 4f;
void Update() {
if (Input.GetMouseButtonDown (0)) {
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mousePos2D = new Vector2 (mouseWorldPos3D.x, mouseWorldPos3D.y);
}
Vector2 dir = Vector2.zero;
RaycastHit2D hit = Physics2D.Raycast (mousePos2D, dir);
if(hit && hit.collider && hit.collider.GetComponent<Rigidbody2D>() != null) {
hit.collider.GetComponent<Rigidbody2D>().gravityScale = 1;
}
if( Input.GetMouseButtonUp (0)){
grabbedObject = null;
}
}
void FixedUpdate() {
if(grabbedObject != null) {
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
Vector2 dir = mousePos2D - grabbedObject.position;
dir *= dragSpeed;
grabbedObject.velocity = dir;
}
}
}
Comment
Did you add any of the debugging calls like I suggested in your other question? Log the values of things, log inside the if (hit...
code to see if the raycast is finding anything. Also, you used to be setting grabbedObject
somewhere, but now you're not.
I'm all good now i just deleted it and did it again thanks for the help anyway