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 JoshMBeyer · May 25, 2014 at 03:01 AM · gameobjectcontrollerchangeuser

How to change what the user is controlling?

I am working on a game and trying to straighten out something basic things. Right now I am up against the task of vehicles. So I have written several controller scripts. Lets say the player is walking and finds a car. Now we want to get in it. So the player walks up to it and a message is displayed saying "Press E to enter". Then what happens?

How can I disable the player completely, enable the vehicle(Controller, camera, and other scripts attached) to be what the user is controlling now. Then when able, press E again to exit. How would I go about doing this? Because I have no idea where to start.

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

1 Reply

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

Answer by Fornoreason1000 · May 25, 2014 at 03:35 AM

Ok there are lots of ways around this one. personally I do not know which is best for your case but I'll how you one way to get you started;

basically you want to check that if the player is about to enter the car the disable him/her motion code. then tell you camera scripts to either go into "car mode" or replace them with your CarCamera Scripts. both of these can be done with boolean variable checking and Add/Remove Components functions.

After that , you want the car to be controller, this is where another Addcomponent function will come in. you basically add your car control script to the car your player just got into.

Alternatively... you can pass both Players And cars to be "conmtrollable" objects, that recive inputs external to a certain instance, whenever you change between the two will cause the other to be disabled.

Heres a example of a way of coding that, NOTE copying and pasting this code Will not work as it is for demonstration purposes.

 abstract class ControllableObject : Monobeahviour {
 public void GamePadInputs(Vector2 Leftstic, Vector2 rightStick);
 }
 //Manages the player globally
 public class PlayerManager : Monobehaviour {
 public static ControllableObject player;
 
 public void Update() {
 //place your left and right stick variables in this code
 player.GamePadInputs(myLeftstick , myRightStick }
 }
 }
 //You player, the one with two legs
 public class Player : ControllableObject {
 //Gets called by PlayerManager
 public override void GamePadInputs(Vector2 Leftstic, Vector2 rightStic) {
 //yor movement
 moveDir = leftstic;
 
 }
 }
 //your car class, the one with four wheels
 public class Car : ControllableObject {

 //gets called inside PlyuerManager if this class is currently marked as player
 public override void GamePadInputs(Vector2 Leftstic, Vector2 rightStic) {
 
 Forwardaccel = Leftstic.y;  
 }
 }

when stuck on logic probelms try drawing flowcharts or block diagrams to guide you. break them down as much as you can and convert them into code.

Let me know of you need more info

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 JoshMBeyer · May 25, 2014 at 04:27 AM -1
Share

That was perfect. Thank you very much.

avatar image Fornoreason1000 · May 25, 2014 at 04:46 AM 0
Share

please post comments as comments ans Answers as Answers( I didn't down vote you btw but its tempting ).

you're welcome nonetheless

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

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

Related Questions

C# Change Script Help 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Change gameobject tag when player collides with another gameobject 1 Answer

tree terrain changes with game object!!! 1 Answer

Unity Controller Support Help 0 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