Hello,
First I am sorry for my english I am learning but if you understand french i make a post there http://urlz.fr/17P1.
So, I am working on a game project and I need to move object with the mouse, there is not the probleme.
The probleme is when i move an object i can put him trought another like the blue object in this picture.
Here is the code for moving object.
using UnityEngine;
using System.Collections;
public class PlacementKapla : MonoBehaviour {
RaycastHit hit; // contient les info du point de collision du raycast
GameObject Kapla; // l'objet kapla
GameObject instantiateKapla; // l'instance de l'objet kapla
GameObject lastKapla;
GameObject kaplaSelected;
GameObject lastKaplaSelected;
int nbKapla; // le nombre de kapla instancié
bool isPlaced = false; // pour savoir si l'instance du kapla est placé ou non
Ray ray; // le rayon
void Start ()
{
Kapla = Resources.Load ("Prefabs/Kapla") as GameObject; // specifie la source de l'objet kapla
nbKapla = 0; // on commence a 0 kapla
}
void Update ()
{
ray = camera.ScreenPointToRay (Input.mousePosition);
placement ();
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Probleme quand aucun objet est selected<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(Input.GetMouseButtonDown(0) && Physics.Raycast (ray,out hit) && isPlaced == true)
{
if(kaplaSelected != null)
{
lastKaplaSelected = kaplaSelected;
}
if(lastKaplaSelected != null)
{
lastKaplaSelected.GetComponent<KaplaSelected>().selected = false;
}
kaplaSelected = hit.transform.gameObject;
kaplaSelected.GetComponent<KaplaSelected>().selected = true;
}
}
private void placement()
{
if(Physics.Raycast (ray,out hit)) // test si il y a une collision ou non
{
if(Input.GetKeyDown (KeyCode.Alpha1))
{
nbKapla++;
lastKapla = instantiateKapla;
if(lastKapla != null)
{
lastKapla.layer = LayerMask.NameToLayer ("Default");
}
instantiateKapla = (GameObject)Instantiate (Kapla, new Vector3(hit.point.x,Kapla.renderer.bounds.size.y /2 + hit.point.y,hit.point.z),Quaternion.identity); // on instencie le kapla que l'ont met directement dans le GameObject "instantiatekapla"
instantiateKapla.layer = LayerMask.NameToLayer ("Ignore Raycast"); //pour que l'objet en cours de deplacement ne soit pas influencé par lui-meme
instantiateKapla.name = "kapla " + nbKapla.ToString ();
isPlaced = false;
}
if(!isPlaced)
{
instantiateKapla.transform.position = new Vector3(hit.point.x,Kapla.renderer.bounds.size.y /2 + hit.point.y,hit.point.z);
instantiateKapla.transform.rotation = Quaternion.FromToRotation (instantiateKapla.transform.up,hit.normal) * instantiateKapla.transform.rotation;
if(Input.GetMouseButtonDown (1))
{
instantiateKapla.transform.Rotate (new Vector3(0,90f,0));
}
}
if(Input.GetMouseButtonDown (0))
{
isPlaced = true;
instantiateKapla.layer = LayerMask.NameToLayer ("Default");
if(lastKapla != null)
{
lastKapla.layer = LayerMask.NameToLayer ("Default");
}
}
}
}
}
Thanks for reading and I hope someone will help me.