- Home /
How to make a drag/drop script only affect objects with a tag?
So, Unity's Standard Assets as script that makes objects with rigidbodies capable of being dragged around with the mouse in 3D. The problem is that I've just started C# because of Unity and all my tries in order to make it only affect objects with a "MOVE" tag have failed. I've searched so much stuff explaining tags but I just can't really adapt to this one. THANK YOU SO MUCH TO ANYONE WHO TRIES TO HELP! The script is there below. Basically, making this only affect rigidbodies tagged with a "MOVE" tag? Thank you!
using UnityEngine;
using System.Collections;
public class DragRigidbody : MonoBehaviour
{
public float maxDistance = 100.0f;
public float spring = 50.0f;
public float damper = 5.0f;
public float drag = 10.0f;
public float angularDrag = 5.0f;
public float distance = 0.2f;
public bool attachToCenterOfMass = false;
private SpringJoint springJoint;
void Update()
{
if(!Input.GetMouseButtonDown(0))
return;
Camera mainCamera = FindCamera();
RaycastHit hit;
if(!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, maxDistance))
return;
if(!hit.rigidbody || hit.rigidbody.isKinematic)
return;
if(!springJoint)
{
GameObject go = new GameObject("Rigidbody dragger");
Rigidbody body = go.AddComponent<Rigidbody>();
body.isKinematic = true;
springJoint = go.AddComponent<SpringJoint>();
}
springJoint.transform.position = hit.point;
if(attachToCenterOfMass)
{
Vector3 anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
anchor = springJoint.transform.InverseTransformPoint(anchor);
springJoint.anchor = anchor;
}
else
{
springJoint.anchor = Vector3.zero;
}
springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;
StartCoroutine(DragObject(hit.distance));
}
IEnumerator DragObject(float distance)
{
float oldDrag = springJoint.connectedBody.drag;
float oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = this.drag;
springJoint.connectedBody.angularDrag = this.angularDrag;
Camera cam = FindCamera();
while(Input.GetMouseButton(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
springJoint.transform.position = ray.GetPoint(distance);
yield return null;
}
if(springJoint.connectedBody)
{
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}
Camera FindCamera()
{
if (camera)
return camera;
else
return Camera.main;
}
}
Answer by ShadowUser19 · Dec 26, 2018 at 05:32 AM
I think your question is already answered here: https://answers.unity.com/questions/537673/raycast-object-tag-check.html
But in case it isn't clear, the raycast method you are using writes to a hit variable:
Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, maxDistance))
This hit variable has a transform object with a tag, so what I might try to do is add an if check like this:
if(hit.transform.tag != "MOVE")
return;
Rather than using tags, you could also go for layer masks:
int movableMask = LayerMask.GetMask("Movable");
if(!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, maxDistance, movableMask))
{
etc...
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Layermask for OnTriggerStay() not working... 1 Answer
C# destroy gameobject with tag 5 Answers
Why does IsSleeping() keep returning false? (Billiard Logic) 0 Answers