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 /
avatar image
0
Question by Frost-Games · Mar 06, 2019 at 12:34 AM · 5.3

Camera.main.ScreenToWorldPoint

I have this scripts, that is a weapon that shots prefabs bullets, but all work correclty in the main, camera, when i reach the boss and the camera switches to him (his room in total) the gun dissapears and doesnt shoot. I found the problem in this line of code:

Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position

It only works under the main camera. How can i alter it slighly to work under any camera i have in the game? been trying usuccesful, probably to simple and right down my nose but cant find it... please help me sorry english is not my native language

public GameObject projectile; public Transform shotPoint; public Animator camAnim;

 public int rotationOffset;

 public float shotDelay;
 public float shotDelayCounter;


 private void Update()
 {
     Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotationOffset);

     if(Input.GetMouseButtonDown(0))
     {
         camAnim.SetTrigger("shake");
         Instantiate(projectile, shotPoint.position, transform.rotation);
         shotDelayCounter = shotDelay;
     }

     if(Input.GetMouseButton(0))
         {
             shotDelayCounter -= Time.deltaTime;

             if(shotDelayCounter <= 0)
         {
             shotDelayCounter = shotDelay;
             Instantiate(projectile, shotPoint.position, transform.rotation);
         }

         }
     }
 }

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 Hellium · Mar 06, 2019 at 07:07 AM

The "dirty way": Finding the camera when the main one is disabled

 private Camera camera;
 
 private Camera Camera
 {
    get
    {
        if( camera == null || !camera.gameObject.activeInHierarchy )
        {
             camera = Camera.main ;

             // You may want another way to retrieve the correct camera
             if( camera == null || !camera.gameObject.activeInHierarchy )
                 camera = FindObjectOfType<Camera>();
        }
        return camera;
    }
 }

 private void Update()
 {
     Vector3 difference = this.Camera.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     // ...
 }


The cleaner way: Using events

 // In your boss manager script
 
 public UnityEngine.Events.UnityEvent OnBossBattleStarted;
 public UnityEngine.Events.UnityEvent OnBossBattleEnded;
 
 private void StartBossBattle()
 {
     // Your code ...
     Camera.main.gameObject.SetActive( false ) ;
     BossCamera.gameObject.SetActive( true ) ;
     if( OnBossBattleStarted != null )
         OnBossBattleStarted.Invoke();
 }
 
 private void EndBossBattle()
 {
     // Your code ...
     Camera.main.gameObject.SetActive( true) ;
     BossCamera.gameObject.SetActive( false ) ;
     if( OnBossBattleEnded != null )
         OnBossBattleEnded.Invoke();
 }

Then, in your player code

  public Camera Camera
  {
     get; set;
  }
 
  private void Update()
  {
      Vector3 difference = this.Camera.ScreenToWorldPoint(Input.mousePosition) - transform.position;
      // ...
  }

Finally, add listeners to the OnBossBattleXXX events and assign the correct camera in your player script

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

102 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

Related Questions

5.3 32 bit editor missing Anroid module? 1 Answer

Scores Api with Facebook sdk 7.3 and Unity 5.3 1 Answer

Is it possible to run one scene in VR and separate camera angles of that scene on another monitor? 0 Answers

IsPointerOverGameObject bugs in 5.3 and newer. Workaround suggestions? 0 Answers

Unity 5.3.4: The type or namespace name `NUnit' could not be found. 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