I have a map where I have a 2d Ship as gameObject, which can be dragged with the finger to travel between islands and do the corresponding activities, when the player finishes playing on the island he returns to the map where the ship is, but when you try to drag it, you can not do it anymore.

for the change of scene use collisions, as shown in the code.

public string name;

public void OnTriggerEnter2D (Collider2D other)
    if (other.gameObject.tag == "Player")

Does anyone know the cause of this error?

The drag code:

public float moveSpeed = 5f;
private Vector3 mousePosition;
private Rigidbody2D _rigidbody;

private void Awake ()
    _rigidbody = GetComponent <Rigidbody2D> ();

private void Update ()
    if (Input.GetMouseButton (0))
        mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        mousePosition.z = transform.position.z;

        Vector3 targetPosition = Vector3.MoveTowards (transform.position, mousePosition, moveSpeed * Time.deltaTime);

        _rigidbody.MovePosition (targetPosition);
  • 1
    \$\begingroup\$ The simplest thing to do is to go through the code with a debugger or to add Debug.Log statements throughout, and check which code gets called, and whether all the components are properly initialized at the time of their usage. After you'll do that you'll either know the source of the issue, or could ask a more concrete question, and not "here's my code, why isn't it working?" \$\endgroup\$ – nihohit Jun 4 '19 at 5:52
  • \$\begingroup\$ I already use Debug.Log, but it does not show why the touch does not work to move the boat with my finger or an analago joystick, so create a movement control with buttons to move the boat and now I can not move if I return to the map general, but in the end I could never avariguar, because the other method did not work. Thanks for the help. \$\endgroup\$ – Nicolas Flores Muñoz Jun 4 '19 at 21:36
  • 1
    \$\begingroup\$ Again: you need to find out the details. Is the "Update" method not called? is it called, but the "if" clause isn't entered? Is it entered, but the position isn't adjusted correctly? \$\endgroup\$ – nihohit Jun 5 '19 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.