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
1
Question by Joe 3 · Apr 25, 2010 at 09:30 PM · controllercar

FPS getting in,out and controlling a vehicle.

Hi I'm making a first person shooter.So far characters can walk around, shoot ai's and pick up stuff but I'd like to let them get in vehicles.I don't have a script for a car or anything so I'd need that too.So overall I need a script to let a character get in ,control, then get out of a car.

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 spinaljack · Apr 25, 2010 at 11:47 PM 0
Share

Don't ask for much do you?

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Azrael · May 05, 2010 at 11:11 PM

Ok, this is just theoric, but it could be something as simple as this.

Create a singleton with a property or a global variable with the name "Controller" where controller is a gameobject

Create a car object (something very simple would do) that can be driven by the player if that gameobject is the same than Controller

if (this==Controller) then ...

Create a character object that can also be controlled by the player if the gameobjeect is the same as controller

Create a camera script that points to "Controller" all the time.

Presto! now all you have to do is a global object or a coroutine that checks a key to switch the controller variable from one gameobject to the another. Neat!

Ok now that you've done that make sure that the key switches from player to car only if they are at a certain distance. (you could even check if you are near the door)

Now for the final trick

Ok one way to do this easily is to simply dissapear the character y not rendering it whenever he is not the controller but lets try something more elegant.

Create a new transform child object in the car and place it where the seat of the player would be lets name this "seat" (it could also be a seat model)

Change the script so it makes the seat transform into the parent object of our character when the car is the controller and place the character at position 0,0,0 (if your character is animated you could make it sit)

character.transform.parent=vehicle.seat; //where seat is the seat transform

Theres a problem though, when you "exit the car" the character would still be in the same position 0,0,0, which means it would still be inside the car, to fix this move it to your left or right vector by a few units (if the character is still sitting make it stand). That way your character will exit the car whenever he is out. (this could also be used in case you decided to just dissapear the character so it moves around with the vehicle)

Thats it, is a pretty simple but functional way to move characters around in vehicles. Try it!

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
1

Answer by Bravini · Oct 24, 2010 at 12:48 AM

to make the character get inside I suggest you use the position of your seat object and move your character there, probably localPosition will be better for this. Once there, change the animation to a "sitting animation" on loop, crossfade it with the character moving his arms a bit(don't loop this one) when you press left and right for a better driving experience. To exit, you can either a: create 3 Random.Ranges with let's say 1,3 and subtract it from the player transform.position to make it exit a few inches from the driver seat, then change the animation back to standing.

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 Matheusfig · Nov 21, 2012 at 07:47 PM

Here is the script, read the script to know what do with the variables:

 var MovScript : Movment;
 var Player : Transform;
 var Speed = 50.0;
 var CarSpeed = 50.0;
 var StopSpeed = 0.0;
 var InCarPosition : Transform;
 var PlayerIn = false;
 var ExitPoint : Transform;
 var CanChange = true;
 var turnSpeed: float = 90;
 
 function Update () {
 
 transform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);
 
 Player = GameObject.FindWithTag("Player").transform;
 
 if(PlayerIn == true){
 Player.transform.position.y  = InCarPosition.transform.position.y;
 Player.transform.position.z  = InCarPosition.transform.position.z;
 Player.transform.position.x  = InCarPosition.transform.position.x;
 }else{
 
 }
 
 if(Input.GetKey(KeyCode.W)&& PlayerIn == true){
 rigidbody.velocity = transform.forward * Speed *Input.GetAxis("car");
 rigidbody.velocity.y = Input.GetAxis("root");
 }
 
 if(Vector3.Distance(Player.position,transform.position) < 5){
 if(Input.GetKey(KeyCode.E)&& PlayerIn == false){
 if(CanChange == true){
 CanChange = false;
 Change();
 MovScript.gravity = 0;
 MovScript.runSpeed = 0;
 MovScript.normalSpeed = 0;
 Player.transform.position.y  = InCarPosition.transform.position.y;
 Player.transform.position.x  = InCarPosition.transform.position.x;
 Player.transform.position.z  = InCarPosition.transform.position.z;
 PlayerIn = true;
 Speed = CarSpeed;
 }
 }else{
 if(Input.GetKey(KeyCode.E)&& PlayerIn == true){
 if(CanChange == true){
 CanChange = false;
 Change();
 MovScript.gravity = 20;
 MovScript.runSpeed = 30;
 MovScript.normalSpeed = 15;
 PlayerIn = false;
 Speed = StopSpeed;
 }
 }
 }
 }
 }
 
 function Change(){
 yield WaitForSeconds(1);
 CanChange = true;
 }
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 Matheusfig · Nov 21, 2012 at 07:47 PM

OBS: MovScript is my variable of movment script of my player

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make a ship parking 2D game 1 Answer

How to use defaut vehicle script for any car asset? 0 Answers

RCC AI Issue 1 Answer

Best Configuration for Edy Vehicle Physics 0 Answers

MMD How to export model and animations to Unity as 3rd person controller? 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