- Home /
Unity 2D Sprite is invisible.
Whenever I attach this script to my game object and I start the game the sprite turns invisible. Everything else seems fine.
public class FollowPlayerBullet : MonoBehaviour {
public float speed;
private Transform lookAtTarget;
void Start()
{
GameObject tar = GameObject.FindGameObjectWithTag("Player");
if (tar)
{
lookAtTarget = tar.transform;
}
}
void FixedUpdate()
{
transform.LookAt(lookAtTarget);
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
It is not the destroy bullet script as it is the same without that script. I tried changing up the sorting layers and that didn't work. The only thing that seems to be causing the sprite to disappear is that script. Also any improvements on the script itself would help too, it should be a bullet that is instantiated and follows the player for five seconds. Thank You
$$anonymous$$aybe it got rotated by the lookAtTarget
transform, and is facing a different axis making it look like it's invisible. Check the gameview in 3D mode at runtime.
Wow so I did check the 3D mode and the sprite was still visible. The sprite seemed to be completely flat. Hmm thanks a lot for that I might be able to fix it now. If you have any ideas of how to fix this it would be helpful. Thanks a lot :).
Answer by snorcack · Nov 10, 2016 at 10:32 AM
I tried the script with the same components and the sprite remains visible to me. However, since you are using the lookat method, most likely the sprite gets oriented orthogonal to the camera and becomes invisible. I modified the script slightly and got this result.
https://gfycat.com/LimitedDamagedCuttlefish
here is the modified method.
Vector3 direction = lookAtTarget.position - transform.position;
Quaternion toRotation = Quaternion.FromToRotation(transform.right, direction);
//transform.LookAt(lookAtTarget,Vector3.right);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, rotateSpeed * Time.time);
transform.Translate(Vector3.right * speed * Time.deltaTime);
Hope this helps !
Thanks this helped a lot. Prior to asking I got really frustrated. To think the answer was this simple is kinda funny. This community is great and I got a response really fast, it makes me just want to code more and make amazing games in the future. Ill be sure to ask more questions if I get stuck.
Thanks so much ^.^
Answer by aditya007 · Nov 10, 2016 at 11:25 AM
@hapynoid continuing my comment. You can solve this problem by following methods
changing the rotation of sprite at runtime (so it won't affect your project atm) and check when it is visible and note down the rotation values. Afterwards, stop the game and add the following line to your script right after
transform.LookAt(lookAtTarget);
transform.rotation = transform.localEulerAngles = new Vector3(x,y,z);
where x,y and z will be angles you noted down at runtime.
This is not the desired way, because it would make your lookAt function render useless, as you'll need to change the rotation, making it face a different way.
You should set all your gameObjects in x-y axis (in 2D), so their's no rotation in z axis. That would make the lookAt function better, because you won't have any z-rotation and your sprite will be visible.
Your answer
Follow this Question
Related Questions
Unity2D resize the sprite 0 Answers
Cant access the Image icon from class 1 Answer
2D GAME (non UI object) for every pc resolution 0 Answers
Fire emblem like path drawing for a 2D tile based strategy game? 0 Answers
If i flip the scale from a sprite, how do i make my game object child flip aswell? 0 Answers