Hi All,
I’ve updated changes I’ve made to the mouse drag script. I’ve also created a new script that sits on my planet object to detect collision. Several new bugs squashed along the way. However now I am left with one. It seems I am not accounting for if the object is not moving. Though I think I am. My debug for when dragging only seams to tick when the object is moved for a few seconds then stops. Here are the related scripts.
-Bit more info… I want to drag a sprite game object over onto another sprite object. When the user lets go of the movable sprite it should set the target sprite as a variable in my fleet script. As well position the icon at the top right corner of the object. The issue is when you move the object into the target sprite and stop moving it. If you let go it just sits there never performing its action. There is a short window in which moving inside the object and letting go works. But if you let go after holding still for more than a second even inside the object. The item sits over the planet and never snaps back or assigns the new object to the variable. I’m sure I’m overlooking something.
MouseDrag.cs
using System.Collections;
using UnityEngine;
public class MouseDrag : MonoBehaviour
{
float x;
float y;
// Update is called once per frame
void Update()
{
x = Input.mousePosition.x;
y = Input.mousePosition.y;
}
void OnMouseDown()
{
//OnMouseDrag();
Debug.Log("MouseDown");//getting old ain't it.
this.gameObject.GetComponent<FleetClass>().BeingDragged = true;
}
void OnMouseDrag()
{
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 7.0f));//The 7.0f z seems to be the only way to keep my object from flying off its z position when dragged. Not sure why as all my objects are at -1. So I guess the difference from the camera or the mouse height??
Debug.Log("MouseDrag");//Lalalala more flow tracking
}
void OnMouseUp()
{
Debug.Log("MouseUP");//let go of me!
this.gameObject.GetComponent<FleetClass>().BeingDragged = false ;//change our drag detection bool back to false so the on stay function will process the player move object request.
}
}
Fleet class
using UnityEngine;
using System.Collections;
public class FleetClass : MonoBehaviour {
public GameObject CurrentPlanet;
public GameObject FleetIcon;
public bool BeingDragged = false;
public bool CollisionDetected = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (!BeingDragged)
{
float x = FleetIcon.transform.position.x;
float y = FleetIcon.transform.position.y;
float z = FleetIcon.transform.position.z;
Vector3 hostPlanetVector = new Vector3(x, y, z);
if (!checkPosition(hostPlanetVector) & !CollisionDetected)
{
snapToPlanet();
}
}
}
public void snapToPlanet()
{
float x = CurrentPlanet.transform.position.x + 1.5f;
float y = CurrentPlanet.transform.position.y + 1.0f;
float z = -1.0f;
Vector3 newMyVector = new Vector3(x, y, z);
Debug.Log("Fleet Snap");
FleetIcon.transform.position = newMyVector;
}
public void MoveFleetTo(GameObject newPlanetToMoveTo)
{
CurrentPlanet = newPlanetToMoveTo;
snapToPlanet();//should snap the object to the top right corner of the new destination planet after it is set. Appears to work but having issues with Collision detection I believe.
}
public bool checkPosition(Vector3 objectToCheck)
{
float x = CurrentPlanet.transform.position.x + 1.5f;
float y = CurrentPlanet.transform.position.y + 1.0f;
float z = -1.0f;
Vector3 hostPlanet = new Vector3(x, y, z);
if (hostPlanet != objectToCheck)
{
return false;
}
else
{
return true;
}
}
}
PlanetLogic.cs
using UnityEngine;
using System.Collections;
public class PlanetLogic : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D objectThatEnters)
{
objectThatEnters.GetComponent<FleetClass>().CollisionDetected = true;
}
private void OnTriggerStay2D(Collider2D objectThatEntered){
bool _isObjectBeingDragged = objectThatEntered.GetComponent<FleetClass>().BeingDragged;//capturing if the fleet object is still being dragged so moving over a planet doesn't register as a move call.
GameObject fleetToMove = objectThatEntered.gameObject;//Storing the game object to be checked and passed through logic.
if (objectThatEntered.tag == "Fleet" & _isObjectBeingDragged)//if the object is of fleet type and is being dragged.
{
//really don't want to do anything if the object is being dragged.
Debug.Log("Fleet Object Dragging in Planet");//Just visualizing when this is firing.
} else if (objectThatEntered.tag == "Fleet" & !_isObjectBeingDragged)// if the object is a fleet object as well as not currently being dragged(set in mouse down and up in mousedrag script.)
{
fleetToMove.GetComponent<FleetClass>().CurrentPlanet = this.gameObject;//Sets the fleet object public variable for planet the fleet is "Orbiting"(sitting) at.
fleetToMove.GetComponent<FleetClass>().snapToPlanet();//Calls my function to find the x,y,z of the planet being orbited. Then transforms my object to a modified position relative to the planet.
Debug.Log("Fleet entered planet");
}
else
{
fleetToMove.GetComponent<FleetClass>().snapToPlanet();//If the object isn't a fleet object or if it is being
}
}
private void OnTriggerExit2D(Collider2D objectThatExited)
{
objectThatExited.GetComponent<FleetClass>().CollisionDetected = false;
}
}