x


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);
    }   
}
}
more ▼

asked Jun 17 '10 at 07:48 AM

Ent gravatar image

Ent
76 12 13 20

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?

Jun 17 '10 at 08:08 AM Marowi

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.

Jun 17 '10 at 08:41 AM Ent

Fair enough.

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

Jun 17 '10 at 09:53 AM Marowi

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.

Jun 17 '10 at 10:08 AM Ent
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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?

more ▼

answered Jun 17 '10 at 08:30 AM

StephanK gravatar image

StephanK
6k 40 53 93

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.

Jun 17 '10 at 08:37 AM Ent

Maybe 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... ;)

Jun 17 '10 at 09:00 AM StephanK

Ive posted the complete script above

Jun 17 '10 at 10:14 AM Ent

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

Jun 17 '10 at 11:35 AM StephanK

Strange, it works when I create a cube and place this in a prefab. But not when I am using a 3DMax Obj file in a prefab. To make a prefab, dont you just 'CREATE PREFAB' and pull the object into the prefab? K, 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.

Jul 13 '10 at 06:34 AM Ent
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1765
x1285
x223
x188
x166

asked: Jun 17 '10 at 07:48 AM

Seen: 1961 times

Last Updated: Jul 13 '10 at 06:37 AM