Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ent · Jun 17, 2010 at 07:48 AM · colliderprefabraycastingraycasthitresources

getting raycast working on prefabs made from resources created at runtime

Hi Im having some problems with created (prefab) gameObjects and a raycast. I've created some cubes in the game with BoxColliders and using:

        if (Physics.Raycast (ray, hit, 100.0)) {
        distanceToGround = hit.distance;
        Debug.DrawLine (ray.origin, hit.point);
    }   

to show me this in the editor. This works fine. I then created a new gameObject:

var myNewObj = Instantiate(newObject,Vector3.zero,Quaternion.identity);

where newObject is a prefab created beforehand from a resource. What do I have to do to get the raycast working on this object? a box collider would do. I tried:

        var myNewObjRigbody = myNewObj.gameObject.AddComponent("Rigidbody");
    myNewObjRigbody.isKinematic = true;
    myNewObj.gameObject.AddComponent("BoxCollider");

The gameObject gets a rigidbody and a boxcollider, but my raycast does not show up? Do I need to reinitiate something for the raycast to work? does the original file have to be somewhere else as in the resource file, with which I created the prefab. I checked the layers aswell, both are in Default.

thxs Ent

Added complete script: its the: Debug.DrawLine (ray.origin, hit.point); not showing up whats bothering me. Im will add a colliderBox to the prefab to check if its something else. The DistanceToGround is confusing because I havent changed the Var name from the example I had, should be DistanceToCamera.

objectscript (JS):

var counter : int = 0;
var modWidth : int = 900;
var modHeight : int = 900;
var modDepth : int = 900;
var modScale : float = 0.005;
var newObject : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")){
    var newPos = new Vector3((modWidth*counter*modScale),0.0,(modHeight*0.5*modScale));
    var newScale= new Vector3(modScale,modScale,modScale);
    var newRot = new Vector3(270.0,180.0,0.0);
    var myNewObj = Instantiate(newObject,Vector3.zero,Quaternion.identity);
    var myNewObjRigbody = myNewObj.gameObject.AddComponent("Rigidbody");
    myNewObj.localPosition = newPos;
    myNewObj.localScale = newScale;
    myNewObj.localRotation = Quaternion.Euler(newRot);
    myNewObjRigbody.isKinematic = true;
    myNewObj.gameObject.AddComponent("BoxCollider");
    counter++;
}
}

objectscript (CS):

int counter = 0;
int modWidth = 900;
int modHeight = 900;
int modDepth = 900;
float modScale = 0.005f;
//GameObject  newObject;
void  Update (){
    if (Input.GetButtonDown("Fire1")){
        Vector3 newPos= new Vector3((modWidth*counter*modScale),0.0f,(modHeight*0.5f*modScale));
        Vector3 newScale= new Vector3(modScale,modScale,modScale);
        Vector3 newRot= new Vector3(270.0f,180.0f,0.0f);
        GameObject myNewObj = Instantiate(Resources.Load("hocker"),Vector3.zero,Quaternion.identity) as GameObject;
        //TYPE?!? myNewObjRigbody= myNewObj.gameObject.AddComponent<Rigidbody>();
        myNewObj.transform.localPosition = newPos;
        myNewObj.transform.localScale = newScale;
        myNewObj.transform.localRotation = Quaternion.Euler(newRot);
        //myNewObjRigbody.isKinematic = true;
        myNewObj.gameObject.AddComponent<BoxCollider>();
        counter++;
    }
}  

camerascript:

function Update(){

var hit : RaycastHit; var ray = Camera.main.ScreenPointToRay (Input.mousePosition); //Just a check if over GUI or not if (!onMouseOverMenu) { if (Physics.Raycast (ray, hit, 100.0)) { distanceToCamera = hit.distance; Debug.DrawLine (ray.origin, hit.point); }
} }

Comment
Add comment · Show 4
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
avatar image · Jun 17, 2010 at 08:08 AM 0
Share

It's not clear to me from your question, but is there a reason why you can't just setup the prefab to have all the components and settings that you require, so any instance you create will already have them?

avatar image Ent · Jun 17, 2010 at 08:41 AM 0
Share

I dont always know which collider fits best beforehand, because the meshs will get changed later. So its more like if its object1 use mesh1 and box, if its object2 use mesh2 and Sphere and if it is object3 use mesh3 and mesh collider. I cant make prefabs for all mesh colliders.

avatar image · Jun 17, 2010 at 09:53 AM 0
Share

Fair enough.

Without testing your code - you're aware that Debug.DrawLine only appears in the Scene View and not in the Game View?

avatar image Ent · Jun 17, 2010 at 10:08 AM 0
Share

like mentioned, I can see it working with the cubes in the scene, but not the inserted prefabs. Ive tried it in CS now, its the same.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by StephanK · Jun 17, 2010 at 08:30 AM

gameObject.layer = numOfLayer;

Regarding your main question we could use some info. You don't say how you create the ray, where your ground is and if/where you move the new object. If your ground is at (0,0,0) and you create a new object at the same position a raycast will probably not hit this object...

I checked your script and it works for me. Only thing I changed was the camera script:

public class CamerScript : MonoBehaviour {

 void Update() {

     RaycastHit hit = new RaycastHit();
     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     //Just a check if over GUI or not

// if (!onMouseOverMenu) { if (Physics.Raycast (ray, out hit)) { float distanceToCamera = hit.distance; Debug.DrawLine (ray.origin, hit.point); }
// } } }

It draws the rays for manually placed items as well as for instantiated prefabs. Maybe you should check out if the colliders are configured correctly on the instantiated prefabs?

Comment
Add comment · Show 5 · 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
avatar image Ent · Jun 17, 2010 at 08:37 AM 0
Share

I am creating multiple pieces next to each other, so at least one should not be at 0,0,0 :) they are not moved. They are near the cubes I created before, so it should be working.

avatar image StephanK · Jun 17, 2010 at 09:00 AM 0
Share

$$anonymous$$aybe you could post the whole code you have right now. (At least the part that is bothering you) Otherwise all we can do is make wild guesses... ;)

avatar image Ent · Jun 17, 2010 at 10:14 AM 0
Share

Ive posted the complete script above

avatar image StephanK · Jun 17, 2010 at 11:35 AM 0
Share

It works for me as it's supposed to...

avatar image Ent · Jul 13, 2010 at 06:34 AM 0
Share

Strange, it works when I create a cube and place this in a prefab. But not when I am using a 3D$$anonymous$$ax Obj file in a prefab. To make a prefab, dont you just 'CREATE PREFAB' and pull the object into the prefab? $$anonymous$$, my mistake :) cant pull resources into the game ^^. Could that be the problem? I just pulled my resource file from the resource folder onto my prefab. It seemed to work but that might be my problem.

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

No one has followed this question yet.

Related Questions

Multiple hit detection with Raycasting? 2 Answers

Raycast Collider Tag not returning correct result 1 Answer

raycastHit.point - How do I make it ignore colliders? 1 Answer

How do I check for collisions when I move objects based on Time.deltaTime? 3 Answers

Raycast exit point of collider 0 Answers


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