bonjour,
Mon but est à travers un exemple simple(un objet thisCollider heurte en meme temps 2 sphères proches otherCollider1 et otherColllider2)de récupérer au moment de la collision multiple
le nombre total de points de contact.
J'attache donc 2 scripts à thisCollider:
collision1.cs
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collision1 : MonoBehaviour
{
public SphereCollider thisSphereCollider,otherSphereCollider1;
Vector3 vect;
int i,;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision otherSphereCollider1)
{
ContactPoint[] contacts = new ContactPoint[10];
int a= otherSphereCollider1.contacts.Length;
Debug.Log("nombre de points de contact: " + otherSphereCollider1.contacts.Length);
Debug.Log("First normal of the point that collide: " + otherSphereCollider1.contacts[0].normal);
for(int i=0;i<otherSphereCollider1.contacts.Length;i++)
{
Debug.Log("vcoordonnées du point de contact "+i+" : ("+otherSphereCollider1.contacts[i].point.x+","+otherSphereCollider1.contacts[i].point.y+","+otherSphereCollider1.contacts[i].point.z+")");
Debug.Log("---------------------------------------------");
}
}
et un autre script collision2.cs
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collision2 : MonoBehaviour
{
public SphereCollider thisSphereCollider,otherSphereCollider12
Vector3 vect;
int i,;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision otherSphereCollider2)
{
ContactPoint[] contacts = new ContactPoint[10];
int b= otherSphereCollider2.contacts.Length;
Debug.Log("nombre de points de contact: " + otherSphereCollider2.contacts.Length);
Debug.Log("First normal of the point that collide: " + otherSphereCollider2.contacts[0].normal);
for(int i=0;i<otherSphereCollider2.contacts.Length;i++)
{
Debug.Log("vcoordonnées du point de contact "+i+" : ("+otherSphereCollider2.contacts[i].point.x+","+otherSphereCollider2.contacts[i].point.y+","+otherSphereCollider2.contacts[i].point.z+")");
Debug.Log("---------------------------------------------");
}
}
Seulement,avec ces 2 scripts attachés à thisCollider
,je n'obtiendrais pas le nombre totals de points
J'essaie d'y remédier en créant un nouveau script recapitulatif_collisions.cs incluant 3 classes:
la classe principale qui hérite à la fois de collision1 et collision2
la classe collision1
la classe collision2
Dans la classe prinicipale,je crée 1 objet OBJ1 de collision1 puis 1 objet OBJ2 de collisioN2 puis je définis:
OBJ1.a
OBJ2.b
pour essayer d'obtenir le nombre total de points de contact:
int c=OBJ1;a+OBJ2.b;
comme ceci:
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class recapitulatif_collisions : collision1,collison2
{
void Start()
{ int c;
collision1 objet1 = new collision1();
collision2 objet2 = new collision2();
c=objet1.a+objet2.b;
c=a+b;
}
// Update is called once per frame
void Update()
{
Debug.Log("nombre total de points de contact :"+c);
}
}
public class collision1
{
public SphereCollider thisSphereCollider,otherSphereCollider1;
Vector3 vect;
int i,;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision otherSphereCollider1)
{
ContactPoint[] contacts = new ContactPoint[10];
int b= otherSphereCollider1.contacts.Length;
Debug.Log("nombre de points de contact: " + otherSphereCollider1.contacts.Length);
Debug.Log("First normal of the point that collide: " + otherSphereCollider1.contacts[0].normal);
for(int i=0;i<otherSphereCollider1.contacts.Length;i++)
{
Debug.Log("vcoordonnées du point de contact "+i+" : ("+otherSphereCollider1.contacts[i].point.x+","+otherSphereCollider1.contacts[i].point.y+","+otherSphereCollider1.contacts[i].point.z+")");
Debug.Log("---------------------------------------------");
}
}
public class collision2 :
{
public SphereCollider thisSphereCollider,otherSphereCollider2;
Vector3 vect;
int j,;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision otherSphereCollider2)
{
ContactPoint[] contacts = new ContactPoint[10];
int b= otherSphereCollider2.contacts.Length;
Debug.Log("nombre de points de contact: " + otherSphereCollider2.contacts.Length);
Debug.Log("First normal of the point that collide: " + otherSphereCollider2.contacts[0].normal);
for(int j=0;j<otherSphereCollider2.contacts.Length;j++)
{
Debug.Log("vcoordonnées du point de contact "+j+" : ("+otherSphereCollider2.contacts[i].point.x+","+otherSphereCollider2.contacts[i].point.y+","+otherSphereCollider2.contacts[i].point.z+")");
Debug.Log("---------------------------------------------");
}
}
Mon problème est que ce dernier script ne fonctionne pas.
Peut-on alors me le corriger ou me donner une autre solution?
merci de votre aide