- Home /
Sprites that moves overlapping
Hi, I'm currently working on a project in which some NPCs are spawned in the scene. These NPCs are represented by Sprites and they move 'walk in a street', moving from one point to another. Their start and end positions are generated randomly:
using UnityEngine;
using System.Collections;
public class ClientSpawner : MonoBehaviour
{
[SerializeField]
private float _spawnTime;
[SerializeField]
private GameObject[] _clientCharacter;
[SerializeField]
private int _clientCount;
[SerializeField]
private Transform _gameArea;
[SerializeField]
private float _padding;
private int _clientsSpawned = 0;
private Vector4 _bounds;
private void Start()
{
Vector3 min = Camera.main.ScreenToWorldPoint( UICamera.mainCamera.WorldToScreenPoint( _gameArea.collider.bounds.min ) );
Vector3 max = Camera.main.ScreenToWorldPoint( UICamera.mainCamera.WorldToScreenPoint( _gameArea.collider.bounds.max ) );
_bounds = new Vector4( min.x + _padding,
max.x - _padding,
min.z + _padding,
max.z - _padding );
if ( _clientCount > 0 )
InvokeRepeating ("Spawn", 0, _spawnTime);
}
private Vector3 SortPosition( bool atRight )
{
Vector3 position;
if ( atRight )
position = new Vector3( _bounds[0], 0f, Random.Range( _bounds[2], _bounds[3] ) );
else
position = new Vector3( _bounds[1], 0f, Random.Range( _bounds[2], _bounds[3] ) );
return position;
}
private void Spawn()
{
_clientsSpawned ++;
if(_clientsSpawned == _clientCount)
CancelInvoke( "Spawn" );
bool atRight = ( Random.Range(0, 2) == 1 );
Vector3 startPosition = SortPosition( atRight );
Vector3 finalPosition = SortPosition( !atRight );
GameObject client = GameObject.Instantiate( _clientCharacter[Random.Range( 0, _clientCharacter.Length )],
startPosition, Quaternion.identity ) as GameObject;
if (!atRight)
{
Flip( client.GetComponentInChildren<Transform>() );
}
client.GetComponent<ClientBehavior>().EndPosition = finalPosition;
client.SetActive( true );
client.transform.parent = transform;
}
private void Flip( Transform t )
{
Vector3 scale = t.localScale;
scale.x *= -1;
t.localScale = scale;
}
}
When they are walking, they may pass through a common position. In this case, however, I have no control of which NPC will be in front of the other, getting some incorrect cases:
How could I correctly treat this? My NPCs have a script in which they only know what should be their final positions:
public class ClientBehavior : MonoBehaviour
{
private Animator _animator;
private int _state; /** 0 (idle), 1 (walking) or 2 (restless) **/
[SerializeField]
private float _velocity;
private Vector3 _endPosition = Vector3.zero;
public Vector3 EndPosition
{
get { return _endPosition; }
set { _endPosition = value; }
}
private void Awake()
{
_controller = GetComponent<CharacterController>();
_animator = GetComponentInChildren<Animator>();
_state = 1;
_animator.SetInteger("State", _state);
}
private void FixedUpdate()
{
if ( _state == 1 )
Walk();
}
private void Walk()
{
transform.position = Vector3.MoveTowards(transform.position, _endPosition, _velocity * Time.fixedDeltaTime);
// Vector3 dir = ( _endPosition - transform.position ).normalized;
// dir *= _velocity * Time.fixedDeltaTime;
// _controller.SimpleMove( dir );
}
}
Thank you.
Answer by Pitomator · Jan 02, 2015 at 04:56 PM
Hi Paulaceccon,
I not 100% sure if I understood your question correctly, but I am assuming that you are having issues with the random drawing order?
A solution to this issue, I used in in past projects, is to set the "Sorting Order" on the SpriteRenderer acording to your characters currenty Y position. So if the character is further up (in the back) it draws them behind the characters that are further down (in front).
Here is a code snapped that does this:
SpriteRenderer sr = gameObject.GetComponent<SpriteRenderer>();
Void Update(){
sr.sortingOrder = (int) (-transform.position.y);
//you may need to multiply this value if an int does not give you enough resolution.
}
Your answer
Follow this Question
Related Questions
Megaman in Unity! Help with movement. 0 Answers
Upon mouseclick, move sprite few pixels to right 1 Answer
Animator warning in editor, animation is preventing script from working 1 Answer
Drag a sprite around with mouse, but have it not overlap other sprites 0 Answers
Why is my quad not moving in sync with its collider? 1 Answer