Skip to content

Instantly share code, notes, and snippets.

@walkies
Created June 26, 2018 04:24
Show Gist options
  • Save walkies/fce6de1c8f5ae12069d07e876c66335c to your computer and use it in GitHub Desktop.
Save walkies/fce6de1c8f5ae12069d07e876c66335c to your computer and use it in GitHub Desktop.
collision example
sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class info
{
[Header("Collision")]
[Tooltip("When colliding with..")]
public string Hit;
public string PlaySound;
private Material Material;
[HideInInspector]
public Collision col;
public UnityEngine.Events.UnityEvent Oncollision;
}
public class Ball : MonoBehaviour
{
private AudioManager audioManager;
private Rigidbody rb;
[Header("Speed Settings")]
public float MinSpeed;
public float MaxSpeed;
private string hit;
[SerializeField]
private info[] info;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (rb.velocity.magnitude < MinSpeed)
{
rb.velocity = rb.velocity.normalized * MinSpeed;
}
else if (rb.velocity.magnitude > (MaxSpeed))
{
rb.velocity = rb.velocity.normalized * MaxSpeed;
}
}
void OnCollisionEnter(Collision c)
{
for (int i = 0; i < info.Length; i++)
{
if (c.gameObject.CompareTag(info[i].Hit))
{
info[i].col = c;
info[i].Oncollision.Invoke();
c.gameObject.SendMessage("TakeDamage", 1, SendMessageOptions.DontRequireReceiver); // change later
Vector3 dir = c.contacts[0].point - transform.position;
dir = -dir.normalized;
rb.AddForce(dir * MaxSpeed / 4);
if (string.IsNullOrEmpty(info[i].PlaySound))
{
return;
}
else
{
AudioManager.instance.PlaySound(info[i].PlaySound);
}
}
}
}
public void Destroy()
{
BallDestroyedData ballDestroyedData = new BallDestroyedData();
//ballDestroyedData variables here
GlobalEvents.OnBallDestroyed.Invoke(ballDestroyedData);
Destroy(gameObject);
//Create();
}
public void Dealdamage(Collision c)
{
c.gameObject.SendMessage("TakeDamage", 1, SendMessageOptions.DontRequireReceiver); // change later
Debug.Log("Do damage");
}
public void ChangeMat()
{
// ball.GetComponent<Renderer>().material = Material;
}
}
/*
* Class of info created
* Array of that info created and serialized
* Ball updates speed
* Info is collected from Array and applied to collision variables
* functions for increased user functionality
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment