// 공
public class ball : MonoBehaviour
{
Vector3 MovePos; // 이동벡터
public float _speed = 4f;
void Start()
{
MovePos = new Vector2(1f, 1f).normalized;
}
void Update()
{
transform.position += MovePos * _speed * Time.deltaTime;
}
}
// 벽
public class wall : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag.Equals("ball"))
{
ball b = collision.gameObject.GetComponent<ball>();
vector3 income = b.MovePos; // 입사벡터
Vector3 normal = collision.contacts[0].normal; // 법선벡터
b.MovePos = Vector3.Reflect(income, normal).normalized; // 반사벡터
}
}
}