My game has two players who spawn in facing each other when the game launches, and each player has a child object with the collision box set up to be where the sword is in the swing sprite and with “Is Trigger” selected in the inspector. I have my code set up to play a sound when the “swing” bool is true and the child collision box detects a collision. As the question states, the collision is only detected when there is movement. Additionally, the child “Is Trigger” collision boxes are detecting each other.
Code is posted below.
Player One
using UnityEngine;
using System.Collections;
public class p1movement : MonoBehaviour {
public float moveSpeed = 10;
float timer = 0.33f;
float reset = 0.33f;
bool swing = false;
public Sprite img1;
public Sprite img2;
void SetSprite()
{
gameObject.GetComponent<SpriteRenderer>().sprite = img1;
}
void SetSprite2()
{
gameObject.GetComponent<SpriteRenderer>().sprite = img2;
}
void Update() {
if (Input.GetKey(KeyCode.A)) {
transform.Translate(Vector3.right * -moveSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate (Vector3.right * moveSpeed * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.J)) {
this.gameObject.GetComponent<SpriteRenderer>().sprite = img2;
swing = true;
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
}
if (swing == true) {
timer -= 1 * Time.deltaTime;
}
if (timer <= 0) {
this.gameObject.GetComponent<SpriteRenderer>().sprite = img1;
swing = false;
timer = reset;
}
}
}
Player One Child
using UnityEngine;
using System.Collections;
public class child1 : MonoBehaviour {
void OnTriggerStay2D(Collider2D other) {
Debug.Log ("C1");
if (swing == true) {
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
}
}
float timer = 0.33f;
float reset = 0.33f;
bool swing = false;
void Update () {
if (timer <= 0) {
swing = false;
timer = reset;
}
if (swing == true) {
timer -= 1 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.J)) {
swing = true;
}
}
}
Player 2
using UnityEngine;
using System.Collections;
public class p2movement : MonoBehaviour {
public float moveSpeed = 10;
float timer = 0.33f;
float reset = 0.33f;
bool swing = false;
public Sprite img1;
public Sprite img2;
void SetSprite()
{
gameObject.GetComponent<SpriteRenderer>().sprite = img1;
}
void SetSprite2()
{
gameObject.GetComponent<SpriteRenderer>().sprite = img2;
}
void Update() {
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.Translate(Vector3.right * -moveSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.Translate (Vector3.right * moveSpeed * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.Period)) {
this.gameObject.GetComponent<SpriteRenderer>().sprite = img2;
swing = true;
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
}
if (swing == true) {
timer -= 1 * Time.deltaTime;
}
if (timer <= 0) {
this.gameObject.GetComponent<SpriteRenderer>().sprite = img1;
swing = false;
timer = reset;
}
}
}
Player 2 Child
using UnityEngine;
using System.Collections;
public class child2 : MonoBehaviour {
void OnTriggerStay2D(Collider2D other) {
Debug.Log ("C2");
if (swing == true) {
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
}
}
float timer = 0.33f;
float reset = 0.33f;
bool swing = false;
void Update () {
if (timer <= 0) {
swing = false;
timer = reset;
}
if (swing == true) {
timer -= 1 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Period)) {
swing = true;
}
}
}
Please point out any other flaw or inefficiency in my code as well, as I am trying to learn. Thank you in advance.