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 mineus64 · Jul 25, 2013 at 06:35 PM · systemvehicleidea

Vehicle System Help

I have this idea for a system that would allow someone to get into and out of a vehicle. The vehicle would have several gameobjects childed to it, one at the location of the driver's seat with the "Driver" tag, and one at each passenger seat with the "Passenger" tag. The vehicle would detect if you were a certain distance away from it, and if you were, and you pressed E, then it would detect the closest gameobject with the "Driver" or "Passenger" tags, and snap your position to it. If you were in the driver's seat, then it would make the vehicle a child of you, and modify the movement script, shown below, to increase your speed and decrease your jump height to 0. If you were in the passenger's seat, then it would make you a child of the vehicle. It would revert the parenting/childing for you if you jumped out.

I have 2 questions, 1: Is it feasible? 2: If it is, then how would I script it?

     public var jumpSpeed:float = 18.0;
     public var gravity:float = 32.0;
     public var runSpeed:float = 15.0;
     public var walkSpeed:float = 45.0;
     public var rotateSpeed:float = 150.0;
      
     private var grounded:boolean = false;
     private var moveDirection:Vector3 = Vector3.zero;
     private var isWalking:boolean = false;
     private var moveStatus:String = "idle";
      
     static var dead : boolean = false;
      
     function Update ()
     {
      
             if(dead == false) {
            
            
            
      
      
             // Only allow movement and jumps while grounded
             if(grounded) {
                     moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
                    
                     // if moving forward and to the side at the same time, compensate for distance
                     // TODO: may be better way to do this?
                     if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
                             moveDirection *= .7;
                            
                     }
                    
                     moveDirection = transform.TransformDirection(moveDirection);
                     moveDirection *= isWalking ? walkSpeed : runSpeed;
                    
                     moveStatus = "idle";
                     if(moveDirection != Vector3.zero)
                             moveStatus = isWalking ? "walking" : "running";
                    
                     // Jump!
                     //if(Input.GetButton("Jump"))
                    
                     if (Input.GetKeyDown(KeyCode.Space))
                    
                             moveDirection.y = jumpSpeed;
             }
            
             // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
             if(Input.GetMouseButton(1)) {
                     transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
             } else {
                     transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
                    
             }
            
            
                    
                    
                    
            
             // Toggle walking/running with the T key
             //if(Input.GetKeyDown("t"))
                     //isWalking = !isWalking;
      
      
            
            
             //Apply gravity
             moveDirection.y -= gravity * Time.deltaTime;
      
            
             //Move controller
             var controller:CharacterController = GetComponent(CharacterController);
             var flags = controller.Move(moveDirection * Time.deltaTime);
             grounded = (flags & CollisionFlags.Below) != 0;
            
      
             }
            
            
             if(Input.GetMouseButton(1) || Input.GetMouseButton(0)) {
                     //Screen.lockCursor = true;
                    
                     Screen.showCursor = false;
                    
                    
                     //var mouse1 = Input.mousePosition.y;
                     //var mouse2 = Input.mousePosition.x;
                    
                     }
                    
                     //Vector3 mousePos = Input.mousePosition;
             else  {
                     //Screen.lockCursor = false;
                     Screen.showCursor = false;
                    
                     //Input.mousePosition.y = mouse1;
                     //Input.mousePosition.x = mouse2;
                    
                     //Input.mousePosition = mousePos;
                    
                     }
            
            
     }
      
     @script RequireComponent(CharacterController)
 
 
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
Best Answer

Answer by Benifir · Jul 25, 2013 at 08:09 PM

Yes, it is completely feasible. I don't know how to un-parent a game object but all you have to do is get the transform of the player and the transform of the seat and check if the distance between the two is less than the allowed distance (make an int/float variable for it). Then its just a simple if statement. You should find game objects with tage and cache the player transform as well as the seats transfrom. For example:

 if((_player.transform.position - _myTransform.position) < maxDistance)
      DoSomethingHere();

This should give you a start to go on (:

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 mineus64 · Jul 25, 2013 at 08:27 PM

Thanks.

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

16 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

[Help] FormatException: Input string was not in the correct format System.Int32.Parse 0 Answers

Accessing Functions from other Scripts 1 Answer

An object reference is required for the non-static field, method, or property 1 Answer

Boat movement, driving and animation 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