• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Camando360 · Jan 07, 2012 at 02:30 AM · movementinputcontrollerspeed

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!

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Jan 07, 2012 at 02:57 AM 0
Share

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!

avatar image Camando360 · Jan 07, 2012 at 05:00 AM 0
Share

True that! Yes The first person controller does have a public forward, back, left and right values.

avatar image syclamoth · Jan 07, 2012 at 05:30 AM 0
Share

That wasn't what I was asking- I was asking about a 'speed' value!

avatar image Camando360 · Jan 07, 2012 at 05:55 AM 0
Share

I'm sorry i meant Public Forward back ect "speed" values. So yes it does have a public speed value.

avatar image syclamoth · Jan 07, 2012 at 06:11 AM 0
Share

So, do you have a reference to the FirstPersonController? What language are you coding in?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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:

  1. make the pickup item trigger

  2. make sure your player has a tag

  3. code time(not something hard)

  4. 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;

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Camando360 · Jan 07, 2012 at 06:42 PM 0
Share

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.

avatar image syclamoth · Jan 07, 2012 at 08:45 PM 0
Share

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.

avatar image Camando360 · Jan 07, 2012 at 09:20 PM 0
Share

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.

avatar image syclamoth · Jan 08, 2012 at 01:17 AM 0
Share

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-

OnTriggerEnter

GetComponent

Remember to use the dropdown to the top-right of the code examples to set the language to C# (since the default is JS).

avatar image Hybris · Jan 08, 2012 at 09:55 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

My Players speed differs depending on the input method (gamepad or keyboard) 1 Answer

Check the latest key pressed? 2 Answers

Controller Joystick Hold Delay Logic? 1 Answer

How to get 8 direction Movement with controller sticks | Topdown 3D 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges