- Home /
 
how to make object follow mouse cursor while detecting collision
Hello Guys, i was trying to make an gameobject follow the mouse cursor while in same time detect collision as i am making some sort of shop in my game i want player to buy things and then place them but the problem is the player can overlap 2 things in each other here is the code i use: void FixedUpdate () { Ray CamRay = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit floorHit;
 
         if (Physics.Raycast (CamRay, out floorHit, camRayLength, floorMask) && boolc1 == true) {
             BluePrint = Instantiate (Computer1, floorHit.point, rot);
             boolc1 = false;
         }
         if (BluePrint != null) {
             BluePrint.GetComponent<Rigidbody> ().MovePosition (new Vector3 (floorHit.point.x,0.7f,floorHit.point.z));
             if (Input.GetKeyDown (KeyCode.R)) {
                 BluePrint.transform.Rotate (BluePrint.transform.rotation.x, BluePrint.transform.rotation.y + 90, BluePrint.transform.rotation.z);
             }
             BluePrint.transform.position = new Vector3 (BluePrint.transform.position.x, 0.7f, BluePrint.transform.position.z);
             if (Input.GetMouseButtonDown (0)) {
                 Instantiate (BluePrint, BluePrint.transform.position, BluePrint.transform.rotation);
                 Destroy (BluePrint);
                 Money -= BpPrice;
                 BuySound.clip = BuyClib;
                 BuySound.Play ();
             }
             PD = BluePrint.GetComponent<PlaceDetect> ();
             PD.IsBluePrint = true;
         }
         MoneyText.text = Money.ToString () + "$";
         Sal.myData._iUser.Money = Money;
     }
 
               and this code attached to the gameobject: using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class PlaceDetect : MonoBehaviour {
     public bool IsBluePrint = false;
 
     void OnCollisionEnter(Collision collision) {
         if (collision.gameObject.tag != "Floor" && IsBluePrint == true) {
             GetComponent<Renderer> ().material.color = Color.red;
             Debug.Log ("Collided with othe object");
         }
     }
 }
 
               note: isBluePrint is getting Triggered from other Script.
               Comment
              
 
               
              Answer by zyad137 · Nov 12, 2017 at 01:06 AM
Fixed it My Self :D
Here is the New Code:
 void FixedUpdate () {
         Ray CamRay = Camera.main.ScreenPointToRay (Input.mousePosition);
 
         RaycastHit floorHit;
 
         if (Physics.Raycast (CamRay, out floorHit, camRayLength, floorMask) && boolc1 == true) {
             BluePrint = Instantiate (Computer1, floorHit.point, rot);
             boolc1 = false;
         }
         if (BluePrint != null) {
             BluePrint.GetComponent<Rigidbody> ().position = new Vector3 (floorHit.point.x,0.7f,floorHit.point.z);
             if (Input.GetKeyDown (KeyCode.R)) {
                 BluePrint.transform.Rotate (BluePrint.transform.rotation.x, BluePrint.transform.rotation.y + 90, BluePrint.transform.rotation.z);
             }
             BluePrint.transform.position = new Vector3 (BluePrint.transform.position.x, 0.7f, BluePrint.transform.position.z);
             PD = BluePrint.GetComponent<PlaceDetect> ();
             PD.IsBluePrint = true;
             if (BluePrint.GetComponentInChildren<MeshCollider> ().isTrigger == false) {
                 BluePrint.GetComponentInChildren<MeshCollider> ().isTrigger = true;
             }
             if (Input.GetMouseButtonDown (0) && PD.CanNotbePlaced == false) {
                 BluePrint.GetComponentInChildren<MeshCollider> ().isTrigger = false;
                 PD.IsBluePrint = false;
                 Instantiate (BluePrint, BluePrint.transform.position, BluePrint.transform.rotation);
                 Destroy (BluePrint);
                 Money -= BpPrice;
                 BuySound.clip = BuyClib;
                 BuySound.Play ();
             }if (PD.CanNotbePlaced == true && Input.GetMouseButtonDown (0) && Timer > 3) {
                 WarningText.text = "The Object Can Not Be Placed Here!";
                 StartCoroutine (WarnWait ());
                 Timer = 0;
             }
         }
         MoneyText.text = Money.ToString () + "$";
         Sal.myData._iUser.Money = Money;
         Timer += Time.deltaTime;
     }
 
               and the other Script:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlaceDetect : MonoBehaviour {
     public bool IsBluePrint = false;
     public Material[] OColors;
     public Material[] OtherColor;
     public bool CanNotbePlaced = false;
     GameObject GMObject;
     GameManger Gm;
 
     void Start () {
         GMObject = GameObject.FindGameObjectWithTag ("GameController");
         Gm = GMObject.GetComponent<GameManger> ();
     }
 
     void OnTriggerEnter(Collider other) {
         if (other.gameObject.tag != "Floor" && IsBluePrint == true) {
             GetComponentInChildren<Renderer> ().materials = OtherColor;
             CanNotbePlaced = true;
         }
     }
     void OnTriggerExit(Collider other) {
         if (other.gameObject.tag != "Floor" && IsBluePrint == true) {
             GetComponentInChildren<Renderer> ().materials = OColors;
             CanNotbePlaced = false;
         }
     }
 }
 
              Your answer