Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by danielgullatte · Jan 03, 2021 at 09:19 PM · prefabfpsinterfacemain camera

How to make an organized, first-person ability system of prefabs when they can't access the main camera?

Hello everyone! I'm relatively new to Unity and C#, but I'm starting on a first-person, fantasy combat game where you essentially map abilities (weapon attacks, spells, bombs, etc) to whichever available buttons you want, enter an arena, then fight monsters with what you chose. If you die, pick new abilities if you want, and try again.

My initial idea is to have every ability be a prefab that includes a logic script which inherits from an interface called IAbility. For example, this ability would let you throw a Bomb prefab: alt text

 public class BombThrow : MonoBehaviour, IAbility
 {
     public float throwForce = 40f;
     public GameObject bombPrefab;
     public void Action()
     {
         GameObject bomb = Instantiate(bombPrefab, transform.position, transform.rotation);
         Rigidbody rb = bomb.GetComponent<Rigidbody>();
         rb.AddForce(transform.forward * throwForce, ForceMode.VelocityChange);
     }
 }

My Player gameobject will have an AbilityController component. That Controller contains GameObject variables that get assigned the ability prefabs I want to use. This is how I plan to keep those assigned abilities between scenes. alt text I then search those slots for any IAbility components and run

 slot1.GetComponent<IAbility>().Action();

to perform the ability.


My problem is this: since it is a first person game, I would need to reference the Main Camera so I can aim a bomb, spell or whatever else in the direction that the character (and subsequently, camera) is facing. But since all my abilities are prefabs, I can't reference the Main Camera in the ability script or prefab.

I feel like this has surely been done before, so are there any suggestions on how these pre-defined abilities could use the main camera?

Thanks for the help!

prefab.png (28.1 kB)
abilitycontroller.png (17.5 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Llama_w_2Ls · Jan 03, 2021 at 09:21 PM

The Main Camera of a scene can be referenced using Camera.Main if you can't assign it publicly, for example, a prefab. @danielgullatte

Comment
Add comment · Show 1 · 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 danielgullatte · Jan 03, 2021 at 11:41 PM 0
Share

Interesting. I added Camera.$$anonymous$$ain to the instantiation line and it worked:

     public void Action()
     {
         GameObject bomb = Instantiate(bombPrefab, Camera.main.transform.position, Camera.main.transform.rotation);
         Rigidbody rb = bomb.GetComponent<Rigidbody>();
         rb.AddForce(transform.forward * throwForce, Force$$anonymous$$ode.VelocityChange);
     }

Before I posted this I was trying to set a Camera variable equal to Camera.main in an Awake method. But when I called slot1.GetComponent<IAbility>().Action(); it would return an error saying that the variable wasn't set.

But this works, so thank you very much for the help! @Llama_w_2Ls

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

182 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 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

Instantiated GameObject does not have variables 0 Answers

instansiating prefab, then adding force in camera direction C# 0 Answers

How to control variable of prefab and its clone 0 Answers

Change GameObject According to UI selection 2 Answers

Why is my camera rotating like crazy? 2 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