Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zyad137 · Nov 11, 2017 at 05:40 AM · collisionrigidbody

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

80 tags for one game? 3 Answers

Rigid Body Collision Detection 1 Answer

Nav Mesh Agent won't fall off the platform? 1 Answer

How can I stop a rigidbody (Ragdoll) character from colliding with the Character Controller capsule? 1 Answer

Waypoint Question(Conflicting Collision) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges