- Home /
Speed pickup
Alright, in my game i want speed boosters that will accelerate my First Person Controler. The logical script i think of is: If Firstpersoncontroler.touches boost then Firstpersoncontroler.speed = 15;
So yeah, i'm not very good at coding yet but i would like to figure this out. Thanks!
Does your first-person-controller have a public speed value? Also, you shouldn't use tags like 'coding'- they're not very useful since 95% of everything on this site has to do with coding!
True that! Yes The first person controller does have a public forward, back, left and right values.
That wasn't what I was asking- I was asking about a 'speed' value!
I'm sorry i meant Public Forward back ect "speed" values. So yes it does have a public speed value.
So, do you have a reference to the FirstPersonController? What language are you coding in?
Answer by Hybris · Jan 07, 2012 at 05:54 PM
Hello Camando,
first of all your variable speed should be public(static). The solution I use with pickups is:
make the pickup item trigger
make sure your player has a tag
code time(not something hard)
apply the script to the pickup
What is use is: function OnTriggerEnter(hit : Collider){ //are you hit? if (hit.gameObject.tag == "insert your tag here"){ //get the speed var in your other script(I will call it FPC now) FPC.Speed = 15; }
This is only when you want the speed to be 15 if you hit if you want to add speed, you have to replace FPC.Speed = 15; to:
//add speed (I will use 5) FPC.Speed += 5;
Ok so here is my code: using UnityEngine; using System.Collections;
public class NewBehaviourScript : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (hit.gameobject.tag == "Player"){
Character$$anonymous$$otor.speed = 15;
}
}
}
I get these two errors: The name hit does not exist in current context. The name charactermotor does not exist in the current context.
See the bit where the 'hit.gameobject.tag' bit was inside of the OnTriggerEnter function? You should read the answers people post, not just skim them.
@Hybris- if the speed value is static, you can only have one first-person controlled object in the scene, ever! What if the game has to be multiplayer? What if the player can inhabit multiple bodies? Using static values is almost never the best idea.
I Wasn't sure what he meant bye the "What is use is: `function OnTriggerEnter(hit : Collider){" part of it. So i added it and i got an error for that whole line in monodev. In unity i get a unexpected symbol error and a Parsing error. I have no idea what a parsing error is.
Do you really know nothing about coding? Obviously the bit in inverted comments was the actual code, and the rest was context. So,
function OnTriggerEnter(hit : Collider)
{
// blah blah this happens when it enters a trigger
}
Unfortunately, this code is in Javascript, and so is useless to you- @Hybris really shouldn't have posted like that. However, it's really pretty straightforward to translate between the two- it's a consistent pattern.
void OnTriggerEnter(Collider hit)
{
// This is in C# now!
}
You should read through the script reference. Just read through all of it, and look at the examples to get a feel for how it all fits together. Here are some relevant pages to get you started-
Remember to use the dropdown to the top-right of the code examples to set the language to C# (since the default is JS).
Srry it didnt see the last comment srry again
Its true that it would not work in multiplayer cuz all player would die at the same time but this is what i use. The code button doesnt work always with me srry for the confusion