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 /
This question was closed May 07, 2018 at 08:15 AM by Myth for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Myth · Feb 01, 2011 at 11:22 PM · cameraobjectsdifferentswapping

Swaping to a camera attached to another object

Looking at other scripts this one should work but it will not change the "state" variable.

I have manually changed this variable in game and the script switches cameras as it should, it just wont do it when I am pushing "e_key"

It shouldn't be a problem as I have doors using the same detection system and they work!

Would be great if anyone could give me a few pointers!

here is the code:

var player_cam : Camera; var cam : Camera;

var state : int = 0;

function Update ()

{

var hit : RaycastHit;

//find forward then test for x meters in front of position if (Physics.Raycast (transform.position, transform.forward, hit, 2))
{ if(hit.collider.gameObject.tag=="Player" && Input.GetAxis ("e_key") ) { if (state == 0) { state = 1; } if (state == 1) { state = 0; }
} }

if (state == 1) { player_cam.GetComponent("Camera").active = false; cam.GetComponent("Camera").active = true; } if (state == 0) { player_cam.GetComponent("Camera").active = true; cam.GetComponent("Camera").active = false; }

}

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

  • Sort: 
avatar image
1
Best Answer

Answer by DaveA · Feb 01, 2011 at 11:37 PM

   if (state == 0)
    {
        state = 1;
    }
    if (state == 1)
    {
        state = 0;
    } 

You set state = 1, then test if state == 1. It always will. You need an 'else' there instead, or just

state = !state; or state = 1-state;

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 Myth · Feb 02, 2011 at 01:20 AM 0
Share

Thanks! - I was obsessing with the detection part.

avatar image
1

Answer by tool55 · Feb 02, 2011 at 06:17 AM

I would use a boolean instead.

var camSwitch : boolean = true;

function Update () { var hit : RaycastHit;

if (Physics.Raycast (transform.position, transform.forward, hit, 2))
{ if(hit.collider.gameObject.tag=="Player" && Input.GetAxis ("e_key") ) { camSwitch = !camSwitch;

player_cam.GetComponent("Camera").active = camSwitch; cam.GetComponent("Camera").active = !camSwitch; } }

}

Also, since you're going to be accessing the other camera often (I presume) you might as well store it as a variable.

var player_cam : Camera;

//Then you can just write

player_cam.active = camSwitch.

You can just drag the camera into the player_cam variable in the inspector.

Comment
Add comment · Show 2 · 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 Myth · Feb 02, 2011 at 11:04 PM 0
Share

The other camera is locked to an array of turrets on a space ship. There will be an average of four turret arrays per ship + Driver/Commander "seat" - I have used an updated version of my script but if you can see that it will become inadequate, please post something - thanks for your post by the way, it has helped me with another script!

avatar image tool55 · Feb 06, 2011 at 05:57 PM 0
Share

Well, if you have multiple turrents and cameras, you can use an array.

myCams : Camera[];

Then cycle through them. myCams [0], myCams [1], myCams [2] etc. You can put as many cameras in the array as you want in the inspector.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How do I make an object appear differently to different players? 2 Answers

How can I turn off gameobjects icons in camera view? 0 Answers

moving objects relative to the player... 1 Answer

Camera looking through objects when touching 1 Answer

How do you dynamicly change a cameras culling mask based on an object selection? 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