scripts enabled/disabled

HelloHelloHello…

Theres a couple of things Id like to ask…

1.If I have a car, via carwizard, and i want to swap the controls from the car to an fps controller prefab, ie drive for a bit, find something, investigate on foot etc…
Whats the best way to switch?

  1. Is it possible to use a collision between 2 objects to either instantiate or destroy a third object at a different transform.

For future tutorials I would love a bit more info on finding with tags and collisions specific to certain objects.oh and the trigger function
thanks heaps
AC
[/code]

Not sure there’s a “best” way for that…many different possibilities. If you put both types of controls in one script, you could have a boolean called onFoot or something, and put “if (onFoot) { }” around the FPS controls with “else { }” around the car controls, and then have a button for switching movement methods, which would swap onFoot between true and false. (I guess check to see if the car’s stopped first before allowing on foot controls? :slight_smile: ). If you had the two types of controls in separate scripts, you could have a third script that toggles the appropriate movement script on or off using the same principle. Or you could use SendMessage. Or…

Certainly. When you have a collision function, it only tells you that a collision has occured, so you can code whatever you want to happen, and wherever and whenever you want it, as a result of the collision.

–Eric

Cheers Eric
so would this be close?

var other: gameObject;
function OnCollisionEnter(collision:Collision){
destroy.Gameobject(other)
}

I can cut and paste some code components, and get a few basics working…Havent found a good javascript book yet, but modding the existing scripts is a big help.
Thanks
AC

Almost…just replace “var other: gameObject;” with “var other: GameObject;” and “destroy.Gameobject(other)” with “Destroy (other)”. Also, if you’re just testing for a collision, you can have “function OnCollisionEnter() {” which is slightly faster (and less typing :wink: ). You only need a variable of type Collision in there if you need to get info about the colliding object (velocity or whatever). I wouldn’t worry too much about the Javascript book, as a lot of stuff (like collisions) is only relevant to Unity and wouldn’t be covered in any books. Although it’s obviously useful to have a solid grounding in Javascript so you know how to do loops, conditionals, functions, and all the other basics of programming. And remember that capitalization is important. (I still get thrown by that sometimes…)

–Eric

Awesome Eric, thanks v much
AC

Hey so how about the process of instantiating, given that the “spawn point” occurs somewhere else in the scene?

var Spawnage; GameObject;
var other: GameObject; 
var spawnpoint: Transform;
function OnCollisionEnter(){ 
destroy.(other);
Instantiate(Spawnage.spawnpoint,transform.position, transform.rotation); 
}

Any tips on how I’m doing with this one?
Thanks
AC

Not quite there. :slight_smile: Let’s examine the Instantiate command:

Instantiate (Object original, Vector3 position, Quaternion rotation)

So there are three things: Object, Vector3, Quaternion. These can be referenced several different ways: from a variable, hardcoded, computed some way, beamed in by thought control…wait, scratch that, OTEE hasn’t finished the ESP plugin yet. :wink: The point is, you need an object, then you need to tell it where to be, then you need to tell it how to rotate. (Although, you can leave out the position and rotation, and just use the object, in which case it will clone that object and keep the same position and rotation.)

Commonly you instantiate gameObjects, but really you can instantiate anything, including scripts. Let’s stick to gameObjects though. Usually you’d probably make a variable to hold the gameObject, which you’ve done by using “var spawnage : GameObject;” So now “spawnage” is a gameObject and can be used in Instantiate: “Instantiate (spawnage);” would just clone it, for example.

For the position, that’s a Vector3, which is the location in XYZ space. This can be from a variable, or hardcoded, or whatever. For example, if you always wanted “spawnage” to appear at location 10, 50, 90, you could write “Instantiate (spawnage, Vector3(10, 50, 90), Quaternion.identity);”. It might be better to use a variable, though, to make it easier to change without recompiling the script…have “var theLocation : Vector3;” at the top of your script, which makes “theLocation” a variable of type Vector3. So now you can enter whatever values you want for the XYZ coordinates in the inspector, and then do: “Instantiate (spawnage, theLocation, Quaternion.identity);” If you use “transform.position” for the location, it uses the Vector3 of whatever location where the object is that the script is attached to. So “Instantiate (spawnage, transform.position, Quaternion.identity);” would make “spawnage” appear at the same position as the object with the collision script on it.

The rotation is similar to the position, but it’s a quaternion. Using “Quaternion.identity” means basically no rotation. You could have “var theRotation : Quaternion;” if you wanted to specify a particular quaternion in the inspector (assuming you actually understand them; I don’t. :slight_smile: ). Then you’d use “Instantiate (spawnage, theLocation, theRotation);” Again, using “transform.rotation” for the rotation uses whatever rotation of the object that the script is attached to.

One way to specify a rotation without messing directly with quaternions would be to do, say, “var spawnClone = Instantiate (spawnage, theLocation, Quaternion.identity);” followed by “spawnClone.transform.Rotate(Vector3.up * 90);” which would make a clone of the “spawnage” gameObject and then rotate it around its up axis (or Y axis) by 90 degrees.

Hope that helps! And that I didn’t make any typos in the code!

–Eric

Hey great Eric, Ill take some time to absorb that, but I pretty much follow most of it…
Thanks! I have something for my brain to eat over the weekend…
AC

An easier way, at least in my experiance, is to declare a var rotation : Vector3 and then use Quaternion.Euler(rotation) in the Instantiate method;

Cool, I figured there was probably a simpler way but hadn’t really looked into it…

–Eric

If you want to define both a position and a rotation, the most intuitive way would be to add an empty GameObject to the scene, attach its transform to a spawnPoint variable.

var spawnPoint : Transform;

The position and rotation will then be accessible as spawnPoint.position and spawnPoint.rotation.

This way you can manipulate the position and rotation by moving the object in the scene view instead of typing coordinates into an inspector,

Thanks for the tips guys, I’ll be trying to assemble an “instantiate somewhere in 3d space” script soon, and will post my best efforts.
I was seeing some farmiliar terms in the .fbxImport window of Maya. Heres a screen shot. Eric is there links’ here between the Quaternion expression and the reference in the fbx import window? I know this might seem a bit weird to ask, as I dont know this stuff too well, but maybe theres some penny that drops when I can see these factors in their holistic entirety…(?)
Cheers
AC

Well, a quaternion is a quaternion. :slight_smile: It’s a way of expressing rotation in a way that doesn’t suffer from the limitations when using eulers, at the cost of being more complicated.

–Eric