My sprite disappears from the camera view after some time moving
I set up a sprite to be moved by touch input, and it simply disappears after some time from the camera view, but not the scene. Here is the moving script:
using UnityEngine;
using System.Collections;
public class Drag2 : MonoBehaviour
{
public GameObject cube;
// Update is called once per frame
public float speed = 0.1F;
void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
//Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
//cube.transform.Translate(touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 0));
cube.transform.position = Vector3.Lerp(cube.transform.position, touchPosition, Time.deltaTime);
}
}
}
And here are some screenshots of before and after the sprite is gone:
Answer by iLucas13 · Aug 01, 2017 at 02:35 AM
Thanks, @Miguel_cs, I solved the issue by messing around with the clipping planes on the camera. Must be some annoying little bug!
Hi @iLucas13 , Could you please let me know the exact fix (the value of the clipping planes) that worked for you? I seem to be facing the same problem even though my camera is at z = -9 (inferior to my sprites and UI objects).
Hi @iLucas13 , Could you please let me know the exact value of the clipping planes that worked for you?
Answer by Miguel_cs · Jul 31, 2017 at 10:49 PM
Well, If I understood your question the problem is that the object is not showing in the game screen despite it being inside the area according to the scene view. If this is the problem then I'm not even gonna read the script cause the movement isn't the problem. So it might be: 1) You didn't set up the layers correctly. 2) The camera is not set to a Z value inferior to the rest of the stuff, e.g. all stuff should be at Z = 0 and camera at Z = -10 I don't think it is 1) cause You dont seem to have any background to be overlaying. But if it were you would have to go tto the sprite renderer in each object and change sorting layer in each one being the moving object over the background
If it is 2) you just have to change the Z values. It may also be something to do with your camera detinitions. prespective vs /orthographic try changing settings there if the rest doesn't work. Right now I don't remember any other cause to your problem
Answer by Carles-Balsach · Jan 07, 2019 at 09:53 AM
When you use Camera.main.ScreenToWorldPoint() the returning Vector3 will have the Z value that the main Camera has.
Therefore, the first you want to do with the returning Vector3 is set the Z to 0.
I had some headache before figuring this out. Hope it helps!
Answer by jamiecorkhill5 · Jul 03, 2018 at 07:10 PM
@MIguel_cs Your answer was fantastic. I have been having the same problem, and it turned out all I had to, as you said, was to move the sprites from Layer 0 to Layer 1.
Thank you very much.
Answer by itchybarn · Feb 17, 2021 at 04:22 PM
I am having this same problem, however changing the layer of my sprite does not help. I have also changed the Z axis to be in front of everything else, my camera is also set to Z -10. Please help! Thanks!
Your answer
Follow this Question
Related Questions
Failed Re pack resources Unity Android 0 Answers
Is It Bad to be Tired of my Own Mobile Game? 1 Answer
How to send notifications while the game is closed? 2 Answers
Black sprites on Android device 1 Answer
Should I leave space for Ad's while designing my game?Also how are they implemented? 0 Answers