Hi everyone
im learning unity and have made a basic car level you can drive around and score points by collecting boxes (cubes with the tag “collectMe”) which works fine with the following script -
var bigBang : GameObject;
var scoreObjectName = "Score";
var scoreScript : Score;
//before we do anything else, find the score object
scoreObject = gameObject.Find(scoreObjectName);
if(scoreObject) {
// Now find the Score script on that object
scoreScript = scoreObject.GetComponent(Score);
}
function OnCollisionEnter (collision:Collision) {
//check and see if the collision has a tag called "collectMe"
if (collision.gameObject.tag == "collectMe"){
//add a point to your score!
if(scoreScript) {
scoreScript.score += 1;
}
//play the sound
audio.Play();
//make a variable for the location of the collided box
var whereExplode = collision.gameObject.transform.position;
//instantiate an explosion using the prefab delcared as "bigBang"
Instantiate (bigBang, whereExplode, Quaternion.identity);
//destroy the collided with object
Destroy (collision.gameObject);
}
}
Now i’m trying to apply this to the FPS contoller prefab, but it does not work. I thought this may be because the FPS controller doesnt seem to have a collider, so i added a child cube (with its box collider) and tried it with the script attached to that, still cant pick up that damn box!
Can anyone guess why this wouldnt work? any help much appreciated!!
Thanks
Will
With the charactercontroller you will have to use OnControllerColliderHit.
However, i would suggest you use triggers for this. Pickup objects usually shouldnt collide with rigidbody or objects anyway.
You can override function OnTriggerEnter () {} for it.
http://unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html
Then you also need the object which is to b e picked up a trigger by checking the colliders isTrigger flag.
Hi Joachim,
thanks for the reply, im trying to change this script using the methods you suggested and i’ve looked at the script reference but cant quite get how to address the cube to pick up - it didnt seem to like my use of tags / gameObject to reference things. (see my first posting of my script), ive now got this -
var bigBang : GameObject;
var scoreObjectName = "Score";
var scoreScript : Score;
//before we do anything else, find the score object
scoreObject = gameObject.Find(scoreObjectName);
if(scoreObject) {
// Now find the Score script on that object
scoreScript = scoreObject.GetComponent(Score);
}
function OnControllerColliderHit (hit : ControllerColliderHit) {
//check and see if the collision has a tag called "Destructible"
if (hit.collider == "Cube"){
//add a point to your score!
if(scoreScript) {
scoreScript.score += 1;
}
//play the sound
audio.Play();
//make a variable for the location of the collided box
var whereExplode = collision.gameObject.transform.position;
//instantiate an explosion using the prefab delcared as "bigBang"
Instantiate (bigBang, whereExplode, Quaternion.identity);
//destroy the collided with object
Destroy (collision.gameObject);
}
}
this gives the following errors (see pic attached)
Thanks
Will
You are using a variable called ‘collision’ but it is not defined anywhere.
Keli is right.
Generally if you have an compile error, double click on the error in the console. This will bring you to the error in the script, in this case change:
Destroy (collision.gameObject);
to:
Destroy (hit.collider.gameObject);
and
var whereExplode = collision.gameObject.transform.position;
to:
var whereExplode = hit.collider.transform.position;
Guys,
thanks for your replies- sadly just dont quite follow what you’re telling me enough to implement it… im pretty new to the software. I’ve now got the following -
var bigBang : GameObject;
var scoreObjectName = “Score”;
var scoreScript : Score;
//before we do anything else, find the score object
scoreObject = gameObject.Find(scoreObjectName);
if(scoreObject) {
// Now find the Score script on that object
scoreScript = scoreObject.GetComponent(Score);
}
function OnControllerColliderHit (hit : ControllerColliderHit) {
//check and see if the collision has a tag called “collectMe”
if (hit.collider.gameObject.tag == “collectMe”){
//add a point to your score!
if(scoreScript) {
scoreScript.score += 1;
}
//play the sound
audio.Play();
//make a variable for the location of the collided box
var whereExplode = collision.gameObject.transform.position;
//instantiate an explosion using the prefab delcared as “bigBang”
Instantiate (bigBang, whereExplode, Quaternion.identity);
//destroy the collided with object
Destroy (hit.collider.gameObject);
}
}
and the errors noted in the attached pic
thanks for listening!
Nevermind, now got this, which works! -
var bigBang : GameObject;
var scoreObjectName = “Score”;
var scoreScript : Score;
//before we do anything else, find the score object
scoreObject = gameObject.Find(scoreObjectName);
if(scoreObject) {
// Now find the Score script on that object
scoreScript = scoreObject.GetComponent(Score);
}
function OnControllerColliderHit (hit : ControllerColliderHit) {
//check and see if the collision has a tag called “collectMe”
if (hit.collider.gameObject.tag == “collectMe”){
//add a point to your score!
if(scoreScript) {
scoreScript.score += 1;
}
//play the sound
audio.Play();
//make a variable for the location of the collided box
var whereExplode = hit.collider.gameObject.transform.position;
//instantiate an explosion using the prefab delcared as “bigBang”
Instantiate (bigBang, whereExplode, Quaternion.identity);
//destroy the collided with object
Destroy (hit.collider.gameObject);
}
}