I’m stuck trying to figure out a basic script. I’ve read a few threads which are similar but have not been able to decipher the answers, and I read or heard onsite here if it takes more than an hour to figure something out, don’t be afraid to come here and ask.
Working through the Roll a Ball tutorial (for reference, question is not specific to the tutorial or I would have posted there), and I have a sphere rolling around picking up cubes. I set a goal of making it five levels as a challenge. The first four levels are separated by 3 gates (cubes, basically) and the 4th and 5th levels by a teleporter and I have a player reset function in case the ball rolls of the board.
I would have thought the teleporter and reset would have been harder to script, but they were easy because the script is attached to the player ball. The problem I have is trying to get the gates to lower. I want to count the pickups and when all the pickups in the level are gone, then the gate drops allowing access to the next area.
I know in my head what I want to do, just can’t seem to figure out the coding. In my imagination I use the onTriggerEnter method, which is already in place, to check if the count is right to then tell the gateOne to drop.
If I put a script on gateOne it either drops immediately, or if I try to reference the pickup count on the player script it tells me it is not a valid variable, or can’t find it. But I think ideally I would prefer the player script to drop the gate so the game doesn’t spend a lot of resources constantly having the gate check the pick up count.
By now I probably have everyone confused, I shouldn’t be so long winded.
Any help pointing me in the right direction would be most appreciated.
In your OnTriggerEnter () you need to grab the script you need from the player object. it’s super late where I am, and I’m responding on my phone, so bare with me.
OnTriggerEnter provides a variable of type Collier, so you can write:
OnTriggerEnter(Collider other)
{
}
Inside of the method, you have to get the component(script) you want from the player.
Keep in mind that the object returned (the variable ‘other’ of type Collider) is the one with your collider, so if your player script is on a child or parent of the collider object, you have to then navigate to the correct object using ‘other’ as your start point. checkout Transform.parent and
GetComponentFromChild <>() if this is the case.
Othetwise, look up GetComponent <>()
Sorry for the lack in direct info, but This should give you a decent place to start.
I’m not clear as to how you want to do this so I’ll provide a couple of answers.
If you simply want to open doors once you have a set number of pickups, in your script add a public reference for each door. In the inspector add the door objects to the script. Then when the pickup count is met do something like…
door1.transform.position = new Vector3(door1.transform.position.x, door1.transform.position.y - 1.0f, door1.transform.position.z); // Assumes you want the door to drop down and 1.0f is enough to fully drop below the floor.
Now if you want to approach the door and if the pickup count is correct, the door opens then you need a collider placed in front of the door. Use OnCollisionEnter or OnTriggerEnter to detect contact with your ball. Use that to open the door as above. Each door needs a collider placed in front of it and each collider should have a reference to the door it is to open. A collider on the other side of the door can be used to close the door behind you. The same code can be used by testing the door position (if it’s closed open it, if it’s open close it).
You can also use door1.SetActive(false) as a method of opening the door, by removing it. You can also use lerp to animate the door over time by providing a closed position and an open position. I think you would be happiest with using lerp as it provides a nice animation and it will also push you down the learning road a bit farther than you might want to travel right now, but you’ll be glad you did. You can always add lerp later on too.
Bill got me pointed in the right direction. I now have the gate opening when the pick ups are collected. I was so close, but just couldn’t seem to get the right code in the right place. I had the right code, just in the wrong places or I had the wrong code in right place, using GetComponent when I should have used gameObject and such.
For now it just changes position. I will look into ‘lerp’ when I get a chance.
Thanks for the explanation Sarfaraaz. I will need to delve further into your explanation.
Just watch out for lerp abuse, which is so common in the Unity community that it even appears in the docs! If you’re ever passing the current position of a thing as any parameter to Lerp, you’re abusing it and will most likely suffer as a result.
@JoeStrout is correct and I’m a self confessed abuser of lerp and slerp. I’m learning Unity through the documentation and examples, if they abuse I abuse, because I don’t know better. Monkey see, monkey do. Don’t be a monkey and listen to Joe, he knows his stuff and seems to always be there to help.