Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Deninja · Jan 10, 2017 at 07:38 PM · scripting problemscripting beginner

How do I give the player different abilities based on what color block they are standing on?

So some teammates and I have to come up with a way for the player to have different abilities based on what color block they are standing on. The player already has the ability to change a blocks color, but we need to have different abilities attached to those specific colors.

For example, if they are standing on a block they colored blue, they will have the ability to jump higher, red they will be heavier, and green they will be faster.

The ability needs to go away when not touching that color too.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Scoutas · Jan 10, 2017 at 08:08 PM

The way I would do it, is to add an enum which would hold the colors that you already have decided on.

enum Colors {NoColor, Blue, Red, Green};

Colors currentColor = Colors.NoColor;

Then, each player could do a raycast down, that would return the color of the block they are standing on, and then update the enum to the color that was returned. In turn, you'd have a method that would be called, which would change up the variables for jumpHeight, speed or weight.

Personally, I'd use a switch, but you can figure out another way to do so.

 switch (currentColor){
     // you could send in the variables in UpdateVariables method - implement it the way you want.
     case Colors.Red:
             UpdateVariables(--variables--);
             break;
     case Colors.Green:
             UpdateVariables(--variables--);
             break;
     case Colors.Blue:
             UpdateVariables(--variables--);
             break;
 }
Comment
Add comment · 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
0

Answer by KoenigX3 · Jan 10, 2017 at 08:10 PM

One solution would be to make prefabs from those blocks, and attach a script to them, which has an integer variable in it. This variable (lets name it 'blockType') will identify the power-up of the block. You could also create a public 'ChangeBlock' method in it, and when you want to change the cube, you should call this method with passing the variables you need.

The player object should have a script too. In this script, you can check the block underneath with casting a ray.

 RaycastHit hit;
 Physics.Raycast(transform.position, -transform.up, out hit);

This function will cast a ray downwards starting from the player transform. It fills the 'hit' instance with data about the first collider it hits. Then you could get the component, and check the block type. After that, it is your job to implement the changes.

 RaycastHit hit;
 if(Physics.Raycast(transform.position, -transform.up, out hit))
 {
     int blockType = hit.transform.GetComponent<BlockScript>().blockType;
 }

Note that if it hits anything else, which has no BlockScript on it, it will throw an error. You may want to create a new layer mask and apply it to the raycasting function. You can find a lot of tutorials about layers.

After this function, you should create an ApplyAbility() function to change the abilities. The function should get the blockType variable as a parameter, and apply changes on the PlayerScript to make it jump higher and so on.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

109 People are following this question.

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

Related Questions

Toggle direction 0 Answers

level manger and starting scene will not work together 0 Answers

Measuring/finding the height of a stack of game objects then recording that number. 2 Answers

Flashlight flickering script? 0 Answers

NavMesh of an automatically generated maze 0 Answers


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