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 /
This question was closed Feb 05, 2020 at 12:09 PM by tormentoarmagedoom for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by FozzGeorge · Feb 05, 2020 at 12:59 AM · movementcharactercontrollerswitch characters

How do I change between characters mid game? Similair to Lego games.

Hello, I'm something of a beginner in Unity and I'm trying to make a mechanic where the player can take control of objects in a scene, preferably at the press of a button (Space) I've looked up tutorials online but a lot of them are using scripts with elements I'm not yet using, such as animators etc. My scene is currently just made up of basic objects, so I can test the mechanic out first. If someone could help out a beginner like me, I'd love you 3000.

Comment
Add comment · Show 5
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 tormentoarmagedoom · Feb 05, 2020 at 01:22 AM 0
Share

you are lost my friend.

Go look that tutorials. Try to learn what you can first, dont try to get what you want i f are not ready to see things you dont understand... Thats learning.

avatar image FozzGeorge tormentoarmagedoom · Feb 05, 2020 at 03:12 AM 0
Share

Fair point! I've actually made some progress since I posted this, and am able to change between two characters when pressing a button! Now I just need to work out enabling it in a certain proximity.. Thanks for the advice!

avatar image JPhilipp FozzGeorge · Feb 05, 2020 at 11:17 AM 0
Share

You may wanna post your solution as answer to your question then (or remove the question), so that others are helped too (or not try to answer it). Good luck!

Show more comments
avatar image FozzGeorge · Feb 08, 2020 at 06:16 PM 0
Share

An Update: Found the answer. This script also includes my movement, so just bear that in $$anonymous$$d. Add a sphere collider to every object you want to become playable, and then add this script to it. $$anonymous$$ake sure the script is unticked, but the bool "selected" is ticked. Oh and of course make sure all playable objects have Player tag. Basically this scripts detects those objects, and when the space button is pressed, the player will switch to a detected object.

[SerializeField] float moveSpeed = 4f;

 Vector3 forward, right;
 public bool selected;
 Rigidbody Rb;
 public GameObject detectedObject;

 void Start()
 {
     forward = Camera.main.transform.forward;
     forward.y = 0;
     forward = Vector3.Normalize(forward);
     right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
     Rb = gameObject.GetComponent<Rigidbody>();
 }

 void Update()
 {
     if (selected && detectedObject != null)  //take object in trigger and enable its Player$$anonymous$$ove script and disable current script
     {
         if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))
         {
             detectedObject.GetComponent<Player$$anonymous$$ove>().enabled = true;
             this.enabled = false;
         }
     }
 }

 void FixedUpdate()
 {
     if (selected)
     {
         Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         Vector3 right$$anonymous$$ovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
         Vector3 up$$anonymous$$ovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical");

         Vector3 heading = Vector3.Normalize(right$$anonymous$$ovement + up$$anonymous$$ovement);

         if ($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) > 0.1f)
         {
             transform.forward = heading;
         }

         if ($$anonymous$$athf.Abs(Input.GetAxis("Vertical")) > 0.1f)
         {
             transform.forward = heading;
         }

         transform.position += right$$anonymous$$ovement;
         transform.position += up$$anonymous$$ovement;
     }
 }

 void OnTriggerStay(Collider other)   //get the playable object and hold it in a variable
 {
     if (other.tag == "Player" && other.gameObject.name != gameObject.name)
     {
         detectedObject = other.gameObject;
     }
 }

 void OnTriggerExit(Collider other)   //empty the variable if the object is no longer in trigger
 {
     if (other.tag == "Player" && other.gameObject.name == detectedObject.name)
     {
         detectedObject = null;
     }
 }

}

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Related Questions

Why is my character controller not working? 1 Answer

Top-Down Movement in Unity 1 Answer

8-Axis 3D Top Down Movement 1 Answer

How to Logically Match Ground Slope While Using This Code? 1 Answer

Movement with AddForce: Wrong Direction 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