- Home /
Dragging Objects Breaks after Building game
The game I'm working on has a mechanic where you can drag around objects in the room. This game and everything in it works in the Unity Editor but not when I build the game for Windows. In the build, you can't drag any objects or anything. This game does support Oculus Rift but I'm not sure if that has anything to do with especially because the game worked perfectly fine before my newest update. I have attempted to look through code and such to see what I've changed and tried switching things backward but it still breaks.
The script used for dragging objects around. using UnityEngine; using UnityEngine.UI; using System.Collections;
public class CameraScript : MonoBehaviour
{
bool objectGrabbed = false;
public static GameObject currentObject;
public static bool dropped = true;
public GameObject currentChild;
public GameObject targetTransform;
public Image crosshair;
public float objectDistance;
public int magnetism;
public float dampening;
public float deadRange;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
Ray cameraRay = new Ray(transform.position, transform.forward);
RaycastHit camHit;
Debug.DrawRay(transform.position, transform.forward);
if (Physics.Raycast(cameraRay, out camHit) && camHit.collider.gameObject.tag == "Interactable" && LetterManager.canMoveAnything == true)
{
crosshair.color = Color.green;
if (Input.GetAxis("Fire1") != 0 && currentObject == null)
{
dropped = false;
objectGrabbed = true;
SetCurrentObject(camHit.collider.gameObject);
}
}
else if (currentObject == null)
{
crosshair.color = Color.white;
objectGrabbed = false;
}
//drops object
if (Input.GetAxis("Fire1") == 0 && currentObject != null)
{
dropped = true;
SetCurrentObject(null);
}
if (Physics.Raycast(cameraRay, out camHit) && Input.GetButtonDown("Fire2"))
{
camHit.collider.gameObject.GetComponent<Activatable>().Activate();
}
objectDistance = objectDistance + Input.GetAxis("Mouse ScrollWheel");
if (objectGrabbed)
{
currentObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
targetTransform.transform.position = new Vector3(Mathf.Sin((180 - transform.rotation.eulerAngles.y) * Mathf.Deg2Rad) * objectDistance + transform.parent.transform.position.x, Mathf.Cos((90 + transform.rotation.eulerAngles.x) * Mathf.Deg2Rad) * objectDistance + transform.parent.transform.position.y, Mathf.Cos(transform.rotation.eulerAngles.y * Mathf.Deg2Rad) * objectDistance + transform.parent.transform.position.z);
currentChild.transform.LookAt(targetTransform.transform);
currentObject.GetComponent<Rigidbody>().AddForce(-currentObject.GetComponent<Rigidbody>().velocity * dampening);
if (Vector3.Distance(currentObject.transform.position, targetTransform.transform.position) > deadRange)
{
currentObject.GetComponent<Rigidbody>().AddForce(currentChild.transform.forward * magnetism);
}
}
}
void SetCurrentObject(GameObject thing)
{
if (thing != null)
{
currentObject = thing;
currentObject.GetComponent<Rigidbody>().useGravity = false;
}
else
{
currentObject.GetComponent<Rigidbody>().useGravity = true;
currentObject = thing;
}
currentChild.transform.parent = thing.transform;
currentChild.transform.localPosition = Vector3.zero;
objectDistance = Vector3.Distance(this.gameObject.transform.position, currentObject.transform.position);
}
}
You need to post code! I can't help if I don't see what is specifically causing the problem. Can you post your code?
I added the script but my problem is I didn't really even edit this code before it broke, so I don't believe it has anything to do with this script. Another guess I have is tags, every movable object is tagged "Interactable" and when I'm in the editor, the Interactable tag is completely gone and every GameObject with that tag now has Undefined as their tags until I play the game and they all go back to Interactable.
What version of Unity are you using? 5.3.1f1? There's bugs, I'd try another version.