• 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
1
Question by cidmodder · Jan 24, 2011 at 06:32 PM · javascriptfpscharactercontrollercarturret

getting out of a vehicle

I have a turret that I want to mount on the side of a ship. When i go up to a computer I want to be able to hit e and the player can take control of it. I have three seperate scripts. the first on goes onto the computer.

var showGUI : boolean; var other : Transform; var controlingturret=false;

function OnMouseEnter() { showGUI = true;

}

function OnMouseExit() { showGUI = false; }

function OnGUI() { if (showGUI) { GUI.Box(Rect(20, 10, 150, 100), "Press E to Enter"); if (Input.GetButtonDown("Use")){ GameObject.FindWithTag("turret1").GetComponent(TurretControl).enabled=true; GameObject.FindWithTag("Player").GetComponent(which).DoSomething(); controlingturret=true; }

}

}

if (controlingturret){ if (Input.GetButtonDown("Use")){ GameObject.FindWithTag("turret1").GetComponent(TurretControl).enabled=false; GameObject.FindWithTag("Player").GetComponent(which).DoSomethingAgain(); controlingturret=false; } }

the second goes onto the player to control the camera selection

var cam1 : Camera; var cam2 : Camera;

 function Start () {
     cam1.enabled=true;
     cam2.enabled = false;

     }

 function DoSomething() {
     cam1.enabled=false;
     cam2.enabled = true;
 }

 function DoSomethingAgain() {

 cam1.enabled=true;
 cam2.enabled=false;

 }

and the third goes onto the turret itself

var horizontalSpeed = 2.0; var verticalSpeed = 2.0; function Start (){ GetComponent(TurretControl).enabled=false; }

function Update () { // Get the mouse delta. This is not in the range -1...1

 var h = horizontalSpeed * Input.GetAxis ("Mouse X");
 var v = verticalSpeed * Input.GetAxis ("Mouse Y");

 transform.Translate (h, 0, v);

}

So far I am able to get into the turret and control it just fine but i'm not able to take control of the player again. Am I going about this the wrong way? Is there a more effective way to do it? Anyone got any help? Thanks

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
Best Answer

Answer by GesterX · Feb 08, 2011 at 10:31 PM

Your last bit of code is not in a function. You should create an Update function like this:

function Update()
{
if (controlingturret){
if (Input.GetButtonDown("Use")){
    GameObject.FindWithTag("turret1").GetComponent(TurretControl).enabled=false;
    GameObject.FindWithTag("Player").GetComponent(which).DoSomethingAgain();
    controlingturret=false;
    }
    }
}
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 cidmodder · Feb 08, 2011 at 11:44 PM 0
Share

thats more or less what i ended up doing but i had to add a little time between when it checks for it.

avatar image
0

Answer by EMac · Jan 24, 2011 at 07:57 PM

It looks like the last block of code in your first script is not inside a function. Remove the '}' just before it.

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 cidmodder · Jan 24, 2011 at 08:08 PM 0
Share

nope. when i do that it it seems like it just goes straight through and starts the control and then turns it off without ever letting me do anything.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

transform code! 1 Answer

Whenever I shoot a bullet from my gun, the bullet disapears. 1 Answer

An instance of type (Script) is required to access non static member (Script) 1 Answer

Checkpoint script? 1 Answer

Rotate character to the moving direction problems? 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