Hi all,
New to Unity and a bit directionless at the moment. I’m making a 2d sidescroller, have all the basics down from the great 2d tutorial which helped a lot but I’m up to abilities and are a bit lost.
The way I’m trying to go about it currently is making a Javascript such as ‘Ability_Crawl’ which I would attach to the player Prefab. Then once it’s all done he’d be able to crawl.
In the ‘Tunnel’ Prefab I was thinking of having a ‘boundary’ that when the player breaches it, it enables the Crawl ability (variable CanCrawl or something in the script). When it moves away, it disables it.
In the player when the ability/fire button is pressed it would check that CanCrawl variable to make sure it can do it before performing the action.
Is this the right way to go about it? Are there any demos that do something similar? I’m not sure how to check if the Player and Tunnel are colliding or how to trigger the script.
Another quick question, say I made the ‘Tunnel’ Prefab but now want to add the bounding box to it (just a cube with materials size=0) that would have the trigger script on it, how can I edit the Prefab? It wants me to override it and they all get changed to the new single object… =/
Any help or point in the right direction would be greatly appreciated,
Many thanks
If i were you i would make a crawl script fot the player that is activated on input. Then you can crawl under anything else you may create later. If you go with the trigger method in the cave prefab put a little OnTriggerEnter function And a OnTriggerExit function, then on the player script for crawling use a static bool with in update the condition of the bool being true will local scale your character controller down (There are some good crouch scripts in the search sector of the forum) and if the condition is false the return to the original scale. Then control the static bool from the trigger by string (ie) CrawlScriptName.crawl = true;
If you edit a prefab and add on to it just create a new prefab. The prefab should only be a single object in the list of assets. Drop one in the game world to be altered and only that one will be altered is the one your dealing with in the hierarchy. Adjusting it wont break the prefab but adding to it will. And if you need to remane it in the hierarchy so you will know that’s the one you altered. That would work too.
Things to remember if you never built a crawl or crouch script (The character controller is just the bounding box of collision that controls the transform if you will. You will need to create a cool looking animation for your character to perform at the same time to look like its crawling.
Also When scaling back to the original scale the controller needs to first be set to the original y position or it will scale through the terrain and fall through.
Here is a crouch script to play with that also scales an array of other game objects (remember to set the original scale)
var isRigidController : boolean;
var controller : CharacterController;
var otherObjects : GameObject[ ];
private var originalScale = 1.0;
private var lowScalePoint = 0.5;
//=====================================================================================//
function Update() {
if (Input.GetKey (“c”)) {//If We hold the C Key Down
if(isRigidController) {//Incase we dont have a Character Controller.
transform.localScale.y = lowScalePoint;
}
GetComponent(CharacterController);transform.localScale.y = lowScalePoint;
for(var o in otherObjects)
o.transform.localScale.y = lowScalePoint*2;
}
else if (Input.GetKeyUp (“c”)) {//If We Lift up on the C Key.
if(isRigidController) {//Incase we dont have a Character Controller.
transform.localPosition.y = 1;
transform.localScale.y = originalScale;
}
GetComponent(CharacterController);transform.localPosition.y = 1;
GetComponent(CharacterController);transform.localScale.y = originalScale;
for(var o in otherObjects)
o.transform.localScale.y = originalScale;
}
}
Hi Black Mantis,
Thanks for that, though in hindsight Crouch wasn’t probably the best ability for me to use in trying to explain the problem. Crouch will definitely be in the game though so thank you for the code snippets, they will be put to good use 
Say another ability is Eat. You only want the character to be able to eat certain things (eg trees, or plates of lasagna) so you would put the invisible bounding box around that so they don’t have to be right at it - is that the right way? make the box the size you want then change the ‘size’ to 0 so it’s invisible?.
Then something like an Edible script on it (the tree/lasagna) that has OnTriggerEnter and OnTriggerExit functions which alter the Ability_Eat script’s variable CanEat that’s on the player?