How to make cursor stay when touch phase ends?

Hello,
So I have 2d shooter game, I am currently trying to make it mobilize it and I ran into a problem when after I shoot and let go (on my iPad) of the screen the cursor snaps back to where it started. Does anyone know how to fix this?
Thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Weapon : MonoBehaviour {
   
    public GameObject projectile;
    public Transform shotPoint;
    public float timeBetweenShots;
    private  float shotTime;
    Animator cameraAnim;
   
   

    private void Start() {
        cameraAnim = Camera.main.GetComponent<Animator>();
    }
   
    private void Update() {
       
        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) -      transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle - 90f, Vector3.forward);
        transform.rotation = rotation;

        if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) {
           
           
            if(Time.time >= shotTime) {
               
                Instantiate(projectile, shotPoint.position, transform.rotation);
                cameraAnim.SetTrigger("shake");
                shotTime = Time.time + timeBetweenShots;
            }
        }
    }
}

Image for context:

I don’t think the code snippet you’ve shared here has anything to do with the cursor, except maybe reading its position to determine where to shoot. Mind sharing the script that deals with the touch input and determining the cursor position?

That is it I believe, but I do have a cursor script that doesn’t have much that seems relevant to this.

Well there’s nothing in that script that seems to move anything. There is also no code that responds to any touching and dragging. The only thing I see here is:

  • rotating the object to face some “shotPoint”
  • instantiating a projectile facing towards that shotPoint when the left mouse button is clicked (I believe that will also detect touchscreen taps).

I presume that the “shotPoint” is the cursor that we’re discussing, and the movement of that object is certainly not handled in this script.

Maybe look at that shotPoint object and see if there are any scripts on it?

Shot point is just where the projectile instantiates at, nothing to do with that. So how exactly can I make it so it wont snap back to where it started?

Can you show or explain what the “cursor” is in this context? Usually there is no cursor in a touchscreen application. A video might be helpful too.

Ah shoot, bad wording, the "cursor’ is the target you see in the picture I showed. My bad, sorry.

That actually doesn’t really clear that much up honestly :eyes:

Is it the red thing or the white thing?
How does it move? Do you just drag it with your finger?

It is the red thing, it moves by dragging it with your finger, and then tapping it to shoot.

Ok so is it a GameObject? Or is it literally just the hardware cursor, the image for which you’ve changed?

If it’s the hardware cursor, I’m not sure there’s much you can do. It’s probably just a quirk of how your particular device handles the cursor, which may be to return it to the center when you release your finger. I would suggest using a “virtual” cursor instead by creating a GameObject with a SpriteRenderer and moving it around based on where the touch position is.

The target is an image, so theres absolutely no code to fix this?

I wouldn’t say that… We still haven’t even established what the target is. You’re saying it’s an image? Like a UI Image?

Yes

Ok if it’s a UI Image then we’re back to square 1. You must have some script which is moving it around the screen. Can you locate that script?

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CursorFollow : MonoBehaviour {
   
    private void Start() {
        Cursor.visible = false;
    }
   
    void Update() {
        transform.position = Input.mousePosition;

    }
}

Its basically part of the cursor just set off.

I think the problem is that you’re just blankly using Input.mousePosition. that obviously won’t return anything sensible if your finger isn’t touching the screen.

You’ll need to use the actual touch input stuff. Start here:

Ok, I got the idea of the code, but how do I combine this with my weapon script because it isnt really working.

Nvm, I fixed it, here is the code. (I have no clue if this is the cleanest way but it worked)

public GameObject projectile;
    public Transform shotPoint;
    public float timeBetweenShots;
    private  float shotTime;
    Animator cameraAnim;
    private GameObject cursor;
 
    private void Start() {
        cameraAnim = Camera.main.GetComponent<Animator>();
        Input.multiTouchEnabled = true;
        cursor = GameObject.FindGameObjectWithTag("Cursor");
    }
 
    private void Update() {
     
        for (int i=0;i<Input.touchCount;++i) {
            if (Input.GetTouch(i).phase == TouchPhase.Began) {
                Touch touch = Input.GetTouch(0);
                cursor.transform.position = touch.position;
                Vector2 direction = Camera.main.ScreenToWorldPoint(cursor.transform.position) - transform.position;
                float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
                Quaternion rotation = Quaternion.AngleAxis(angle - 90f, Vector3.forward);
                transform.rotation = rotation;
             
                if(Time.time >= shotTime) {
                    Instantiate(projectile, shotPoint.position, transform.rotation);
                    cameraAnim.SetTrigger("shake");
                    shotTime = Time.time + timeBetweenShots;
                }
            }
        }
     

        if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) {
         
            if(Time.time >= shotTime) {
             
                Instantiate(projectile, shotPoint.position, transform.rotation);
                cameraAnim.SetTrigger("shake");
                shotTime = Time.time + timeBetweenShots;
            }
        }
     
    }