I’m making a top down game where I want combat to be run by hitboxes. I have in my combat script that where ever the player clicks the player sprite will face that direction and I want to be able to spawn In the hitbox prefab in front of where the player is facing whether that be forwards, backwards, up, down where ever. If you guys think I can do it easier without having to use a hitbox prefab please tell me about that but heres my code.
public int Health = 10;
public int rotationTime = 1;
public GameObject[] m1Prefab;
public Vector2 xRange;
public Vector2 yRange;
public float spawnRate;
public float distanceToSpawnFromPlayer;
public Transform player;
// Update is called once per frame
IEnumerator rotateTime(){
//rotation
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
yield return new WaitForSeconds(rotationTime);
transform.rotation = Quaternion.identity;
}
void Update()
{
if (Input.GetButtonDown("Fire1")){
Spawn(m1Prefab[Random.Range(0, m1Prefab.Length)]);
StartCoroutine(rotateTime());
}
}
void Spawn(GameObject spawnObject)
{
Vector3 spawnPos = player.position;
spawnPos.x += Random.Range(xRange.x, xRange.y);
spawnPos.y += Random.Range(yRange.x, yRange.y);
spawnPos.z += distanceToSpawnFromPlayer;
Instantiate(spawnObject, spawnPos, Quaternion.identity);
}
When I have a player object and I need to know what is in front of it, I store a private Vector2 called “facingVector” or something to that effect.
Every frame I get a new movementVector from input, and if movementVector is not equal to Vector2.zero AND is not equal to facingVector, I set facingVector equal to movementVector.
That way I always know what direction the player is facing.
Then if I wanted to spawn a gameObject in the direction the player is facing, I would use facingVector, and give it some distance, one or two units or whatever.
That said, for hit detection I think this is an extremely inefficient way to do it, I’m wondering if RaycastHit2D might be what you’re looking for?
It is in the Unity docs and is probably a good place to start.
For RaycastHit2D wouldnt it only see whats directly in front of the line instead of say an area of potential colliders/rigidbodies that should be hitting?
You’ve tagged this as 2D but your spawn code is using Z as “distanceToSpawnFromPlayer” which TBH isn’t making sense to me. 2D is on the XY plane only so are you using Z as rendering layers only in which case it does make sense.
I see you’re also modifying the Transform so I hope you’re not using 2D physics here as you should never do that. You use a Rigidbody2D with the appropriate body-type and use its API to cause movement.
You don’t state what you mean by “hitboxes” so I can only assume you mean a 2D collider set as a trigger. If that’s the case then you’re making it even harder by manipulating the Transform then waiting for the next simulation step (during FixedUpdate by default) to get a callback to see if you overlapped something.
You can simply perform one of the many, many queries to detect other colliders anywhere you like whether that be ray/shape casts or overlap queries. You don’t need to wait for actual contacts/triggers etc.
That could definitely be right, I use a few different casts (Physics2D.Raycast, Physics2D.CircleCast, Collider2D.cast, etc) and I forget which ones return a RaycastHit2D vs a Collider2D.