Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NanjaKilo · Mar 22, 2015 at 04:03 PM · raycastcontrollertagsweaponsmultiple cameras

Raycasting Weapons with two cameras?

Thank you in advance to anyone reading this who can help! I am trying to make a "Battlemech" game in which the upper body of the mech will rotate independently of the torso and legs. The only way I could manage to do that without invoking the clumsiness of the the Self-relative WASD controls in Unity's Third Person Controller was to have a two-camera character rig. The current setup is with the Main Camera parented directly to the Third Person Controller and the secondary camera through the upper body which contains mouse rotation scripts and is also parented through the rest of the model to the Third Person Controller. I've noticed that when trying now to use ray-casting it's a "pick one or the other" scenario. In one case, if the upper body cam is the tagged the Main Camera I end up with an ambiguous sense of turning that rotates the rest of the model in step with the upper body thanks to the 3PC. And if I tag the Torso and leg camera as the Main Camera, all ray-casting happens in the direction of the legs and torso instead of the independently rotating upper body. My work around for this was somewhat of a gamble, I would choose to keep the Main camera with the legs and torso and only call upon the other camera when the player left-clicked to "fire a weapon" having the cameras alternate fast enough, so as to have the steering issue not be of any noticable detriment to the player. I wrote this script and it compiled, but when I run it in game, the two cameras flash back and forth in a sort of feedback loop, and when I cast the ray it either gets rid of both camera entirely or wont fire again... Anyone's help is greatly appreciated, whether it's a fix to my current issue, or a cleaner way of going about this, I'm all ears... #pragma strict

 public var Effect : Transform;
 public var TheDamage = 100;
 public var Cam1 : Camera;
 public var Cam2 : Camera;
 
 function Start()
 {
     Cam1.enabled = true;
     Cam2.enabled = false;
 }
 
 function Update ()
 {
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
     
     if (Input.GetMouseButtonDown(0))
     {
         Cam1.enabled = !Cam1.enabled;
         
         if (Physics.Raycast (ray, hit, 100))
         {
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
         }
     }
         
     else (Input.GetMouseButtonDown(0));
     {
         Cam2.enabled = !Cam2.enabled;
     }
     
 }
Comment
Add comment · Show 1
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 maccabbe · Mar 22, 2015 at 04:12 PM 0
Share

I'm not sure what you are trying to do with your else statement

 else (Input.Get$$anonymous$$ouseButtonDown(0));
 {
      Cam2.enabled = !Cam2.enabled;
 }

But it worries me that the else line ends with a semi colon (;) which makes the code evaluate the condition then continue to the next line regardless of the truth of the condition. In addition the else statement uses the same condition as the if statement which makes it seem like you don't want the else statement to ever be called.

First of all Camera.ScreenPointToRay does not need to be called using Camera.main.ScreenPointToRay. In your case you can use Cam1.ScreenPointToRay() or Cam2.ScreenPointToRay().

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NanjaKilo · Mar 27, 2015 at 04:41 PM

Thanks So much! Its fixed now! The reason I wanted to have the else there was so that in all other cases of the left mouse button being down it would still refer to the 1st lower body camera, that way whenever the layer fired a weapon there was a 100% of it returning to the original setup. with your feedback I saw that it was redundant. I kept the two cam variables. replaced camera.main. with Cam2, gave cam 2 a different tag to prevent conflicts, and deleted the else statement and Function Start. Now everything works swimmingly, thank you!

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

2 People are following this question.

avatar image avatar image

Related Questions

Clicking multiple objects with same tag, how to "forget" the one clicked previously... 0 Answers

Detecting tags on an animated gameobject 0 Answers

Raycasting and receiving hit.colliders with two cameras 0 Answers

Comparing tag not working 0 Answers

Raycast ignore player 3 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