- Home /
Why can't the camera render the sprite?
I have a game I'm making with Unity2D and I coded an enemy. I had programmed it and I wanted to track it's movement by making it render a sprite. The problem is the sprite doesn't show when you're playing the game. This is a problem because the enemy is invisible. It shows the sprite on the scene view but it is invisible in the game view. I am using the same sprite renderer component (I literally copied and pasted it) of my main character who does show up in the game. What could be wrong with my enemy? This is the code in the script attached to my enemy: using UnityEngine; using System.Collections;
public class EnemyScript : MonoBehaviour {
public float maxSpeed = 1f;
bool facingRight = true;
public LayerMask whatIsGround;
float groundRadius = 0.2f;
public Transform GroundCheck;
public bool grounded = false;
public bool Dead = false;
public GameObject player;
float originalmove;
float newmove;
Vector3 BackupPosition;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
originalmove = 1f;
}
// Update is called once per frame
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (GroundCheck.position, groundRadius, whatIsGround);
if (this.transform.position.x < player.transform.position.x)
newmove = originalmove;
if (this.transform.position.x > player.transform.position.x)
newmove = originalmove * -1;
rigidbody2D.velocity = new Vector2 (newmove, rigidbody2D.velocity.y);
anim.SetFloat("Speed",Mathf.Abs(newmove));
if (newmove > 0 && !facingRight)
Flip ();
if (newmove < 0 && facingRight)
Flip ();
if (Dead)
Die ();
if (!grounded)
this.transform.position = BackupPosition;
BackupPosition = this.transform.position;
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void Die()
{
}
}
Answer by DarKTower · Jul 09, 2014 at 03:36 AM
A couple things you should check for:
Make sure the Enemy is spawned (Using Instantiate?) on a Z point that is viewable by the camera. A couple times this has happened and it's because I spawn something on the same Z position that the Camera is on (Usually -10 or something), instead of spawning at 0 or something more than -10.
Make sure the Enemy's Layer has not been set to something that the Camera does not render. This is highly unlikely, but it could've happened.
Check that the Sprite Renderer's checkbox is enabled, and that nowhere you set renderer.enabled to false. I don't see it in your code, so this is highly unlikely.
It's most likely the first one, but if it's not any of the above then I suggest somebody more knowledgeable with Unity answer your question than simple old me.
thank you so much! I always forget that even in Unity2D I have to deal with the Z axis
Thanks! That did the trick: Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition); pos.z = 0; <--- By adding this, the objects became visible Instantiate (wheelCW, pos, Quaternion.Euler (0, 0, 0));
Answer by purnjay · Jun 10, 2016 at 12:10 PM
Go to the camera and change the clipping planes near to something like -67