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 09millarda · May 05, 2012 at 05:28 PM · javascriptgettankhelicopter

Script for getting in and out of a tank

Hi there, I am making an FPS game and I need to add tanks and helicopters. But the thing is, i don't know how to write a script that would allow me to enter a vehicle and get out of a vehicle when pressing the button "e" (E = Enter). Can you please tell me what to do and post a script please. I have been searching on the internet for 2 hours now and everything related that i find, i don't understand it or it isn't what i needed.

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
1

Answer by aldonaletto · May 05, 2012 at 07:15 PM

Since effectively entering the tank is a complex job, a simpler solution is to deactivate the original character and activate a new one inside the vehicle - the vehicle itself is actually a specialized character, whose hierarchy should be something like this:

Tank <- "empty object" with rigidbody and collider matching the tank dimensions TankModel <- tank model Main Camera <- the tank camera

The tank script must take care of the FPS character / tank driver switching, as well as the other movement and firing controls:

var player: Transform; // drag the player here var speed: float = 5.0; // tank moving speed var turnSpeed: float = 30; // tank turning speed in degrees/second var enterRange: float = 8.0; // distance to enter the tank var exitPos = Vector3(0, 1.5, 0); // exit position relative to the tank

private var cam: Camera; // tank camera private var inTank = false; // tells when the character is inside the tank

function Start(){ cam = GetComponentInChildren(Camera); cam.enabled = false; // make sure the tank camera is off }

function EnterTank(){ // enters the tank: player.gameObject.SetActiveRecursively(false); // deactivate the original player... cam.enabled = true; // and activate the tank camera... inTank = true; // and controls }

function ExitTank(){ // move the player to its exiting position: player.position = transform.TransformPoint(exitPos); player.gameObject.SetActiveRecursively(true); // reactivate the original player... cam.enabled = false; // and deactivate the tank camera... inTank = false; // and the tank controls }

function Update(){ if (inTank == false){ // if outside the tank... // and E pressed while player inside the enter range: if (Input.GetKeyDown("e") && Vector3.Distance(player.position, transform.position) turnSpeed Time.deltaTime, 0); var vel = transform.forward Input.GetAxis("Vertical") speed; rigidbody.velocity = Vector3(vel.x, rigidbody.velocity.y, vel.z); // place here other control code, like turret rotation, // cannon elevation, firing etc. } } }

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
avatar image
0

Answer by Piflik · May 05, 2012 at 06:09 PM

If you come across a problem, just think about what has to happen. In this case:

  1. check if the player is looking at a tank/helicopter and is close enough

  2. hide the tank/helicopter model

  3. put the camera on the tank's/helicopter's position/rotation

  4. change the control scheme from standard FPS to tank/helicopter

Putting this in code-form might not be trivial, but also not too hard, methinks.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefabs & scripts 1 Answer

Automatically uncheck? 1 Answer

How to optimize this script and add items imediately without grids 0 Answers

DecoratorDrawer javascript examples 0 Answers

How to save data on iOS? 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