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 tYn0 · Dec 27, 2014 at 08:01 PM · platformmovingswitchdoors

Moving platform with button

So I want to make a platform that will move from point A to B and vice versa when players pushes the button. I edited the script from this tutorial https://www.youtube.com/watch?v=U1UkKw12pQo and it seemd to work ok. But it doesn't work properly on the Z axis for some reason. Platform just moves forward, passing through point where it should stop. I am referring to the platform as a "doors" in my script because I was going to use it as a doors in my game. Thank you for your help.

 using UnityEngine;
 using System.Collections;
 
 public class Doors : MonoBehaviour {
 
     public bool stav; //true - opened, false - closed
     public Transform opened; //Opened state
     public Transform closed; //Closed state
     public float speed;    
     public Transform door;    //Door object
     public Vector3 direction;    //move direction
     
 
     void FixedUpdate () {
     
         if(door.rigidbody.position == opened.position){SetDirection(closed);} //If doors are open, set direction to closed
         if(door.rigidbody.position == closed.position){SetDirection(opened);}    /If doors are closed, set direction to opened
 
         if(stav == true && door.rigidbody.position != opened.position){Move();}  //If doors should be opened but they aren't, call Move()
         if(stav == false && door.rigidbody.position != closed.position){Move ();} //If doors should be closed but they aren't, call Move()
 
 
     }
     
     void Move()
     {    
             door.rigidbody.MovePosition(door.rigidbody.position + direction * speed * Time.deltaTime ); //Door move function
     }
 
     void SetDirection(Transform dir)
     {
         direction = (dir.position - door.rigidbody.position).normalized; //Direction set
 
     }
 
     void Prepni()
     {
         if (stav)
         {
             stav = false;
         }else
         {
             stav = true;
         }
     }
 
     void Nastav(bool nastavit)    //Nastavi stav dveri podla "nastavit"
     {
         if (nastavit){stav = true;}
         else {stav = false;}
 
     }
 
 
     void OnDrawGizmos(){
         Gizmos.color = Color.green;
         Gizmos.DrawWireCube(opened.position, door.transform.localScale);
         
         Gizmos.color = Color.red;
         Gizmos.DrawWireCube(closed.position, door.transform.localScale);
     }
 
 }
 

 
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
0
Best Answer

Answer by george_vasilchenko · Jan 02, 2015 at 08:46 PM

Hey man, I watched your video and I saw that I could apply a different approach. I wrote a script that will satisfy all your needs at once. (The script should be in one .cs file, don't care about two classes in it) The script has ability to:

  1. Work automatically (just moving back and forth with no interaction needed from player);

  2. Work manually (you simply click a mouse button to open or close the door/platform);

  3. Work remotely (in your case it's a button object that tells an obstacle to move away (trigger)).

In order to apply the script, you have to have:

  1. Player object with collider and rigidbody attached and mass set to something small (1-10);

  2. Platform object (no components needed, they will appear automatically);

  3. startPoint object, just transform;

  4. endPoint object, just transform;

  5. button object with BoxCollider attached and aligned desired way;

After you dragged and dropped the script onto your "platform" game object:

  1. assign "startPoint" object to the field "Start Point";

  2. assign "endPoint" object to the field "End Point";

  3. choose desired axis of interaction from the dropdown menu "Used Axis";

  4. choose desired behavior from the dropdown menu "Use Case";

  5. assign your "button" object to the field "Button";

  6. assign your "Player" object to the field "Player";

Before you push play, you choose axis that you want to restrict your platform movement to. Moreover, you choose the desired behavior case, in your case it is "Remote". Should work. I will give you a link to the video of it... Let me know if it helped. :)

The script:

 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(BoxCollider))]
 [RequireComponent(typeof(Rigidbody))]
 public class Platform : MonoBehaviour 
 {
     public Transform startPoint;
     public Transform endPoint;
     public enum UsedAxis { X, Y, Z, X_Y, X_Z, Y_Z, X_Y_Z}
     public UsedAxis usedAxis;
     public enum UseCase { Auto, Manual, Remote}
     public UseCase useCase;

     public Collider button = null;
     public GameObject player = null;

     private float _moveSpeed = 0.75f;
     private bool _return;
     private bool _switch;
     private Rigidbody _platformRigidBody = null;
     private Transform _platform;
     private ButtonSignal _buttonSignal;

     void Start()
     {
         _platformRigidBody = transform.rigidbody;
         _platformRigidBody.mass = 2000;
         _platformRigidBody.isKinematic = true;
         _platformRigidBody.useGravity = false;
         _platform = transform;

         player.gameObject.tag = "Player";

         if (useCase == UseCase.Remote)
         {
             button.gameObject.AddComponent<ButtonSignal>();
             _buttonSignal = button.GetComponent<ButtonSignal>();
         }

         InitializeWorkingAxis();
     }

     void Update()
     {
         if (Input.GetMouseButtonDown(0)) _switch = !_switch;
     }

     void FixedUpdate()
     {
         if (useCase == UseCase.Auto)
         {
             if (_platform.position == endPoint.position) _return = true;
             if (_platform.position == startPoint.position) _return = false;

             if (_return) _platform.position = Vector3.MoveTowards(_platform.position, startPoint.position, _moveSpeed * Time.fixedDeltaTime);
             if (!_return) _platform.position = Vector3.MoveTowards(_platform.position, endPoint.position, _moveSpeed * Time.fixedDeltaTime);
         }
         if (useCase == UseCase.Manual)
         {          
             if (_switch) _platform.position = Vector3.MoveTowards(_platform.position, startPoint.position, _moveSpeed * Time.fixedDeltaTime);
             if (!_switch) _platform.position = Vector3.MoveTowards(_platform.position, endPoint.position, _moveSpeed * Time.fixedDeltaTime);       
         }
         if (useCase == UseCase.Remote)
         {
             if (_buttonSignal._switch) _platform.position = Vector3.MoveTowards(_platform.position, startPoint.position, _moveSpeed * Time.fixedDeltaTime);
             if (!_buttonSignal._switch) _platform.position = Vector3.MoveTowards(_platform.position, endPoint.position, _moveSpeed * Time.fixedDeltaTime);
         }
     }
     void OnDrawGizmos()
     {
         Gizmos.color = Color.green;
         Gizmos.DrawWireCube(startPoint.position, transform.localScale);

         Gizmos.color = Color.red;
         Gizmos.DrawWireCube(endPoint.position, transform.localScale);
     }
     void InitializeWorkingAxis()
     {
         switch (usedAxis)
         {
             case UsedAxis.X:
                 _platformRigidBody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
                 break;
             case UsedAxis.Y:
                 _platformRigidBody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
                 break;
             case UsedAxis.Z:
                 _platformRigidBody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY;
                 break;
             case UsedAxis.X_Y:
                 _platformRigidBody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
                 break;
             case UsedAxis.X_Z:
                 _platformRigidBody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY;
                 break;
             case UsedAxis.Y_Z:
                 _platformRigidBody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX;
                 break;
             case UsedAxis.X_Y_Z:
                 _platformRigidBody.constraints = RigidbodyConstraints.FreezeRotation;
                 break;
             default:
                 _platformRigidBody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
                 break;
         }
     }
 }

 [RequireComponent(typeof(BoxCollider))]
 public class ButtonSignal : MonoBehaviour
 {
     public bool _switch = false;
     private BoxCollider _buttonTrigger;

     void Start()
     {
         _buttonTrigger = GetComponent<BoxCollider>();
         _buttonTrigger.isTrigger = true;
     }

     void OnTriggerEnter(Collider c)
     {
         if (c.gameObject.tag == "Player")
         {
             _switch = !_switch;
         }
     }
 }
Comment
Add comment · Show 19 · 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 tYn0 · Jan 03, 2015 at 03:11 AM 0
Share

Thank you for your script, it works as intended but the smoothness of the doors is a problem for multiple reason. It has really nice effect but that's not what I need. I would rather use rigidbody.$$anonymous$$ovePosition ins$$anonymous$$d of Vector3.Lerp. The problem is that I am having hard time making it togglable. What I need is basically the script in original tutorial, just togglable with bool variable

avatar image george_vasilchenko · Jan 03, 2015 at 09:11 AM 0
Share

Hi, will take a look.

avatar image george_vasilchenko · Jan 03, 2015 at 01:09 PM 0
Share

Sorry, can't help you here, movePosition() + bool isn't very friendly in case of checking positions on frame rate basis. Can you tell why can't you use Vector3.Lerp? What would be this unique purpose to use movePosition()? What advantage does it give you? Besides, the intention of the tutorial was to make a platform, not a door. And platform is meant to move in ping-pong loop, afaik..

avatar image tYn0 · Jan 03, 2015 at 04:44 PM 0
Share

$$anonymous$$y player is a sphere with rigidbody component so if I want to use it as a elevator for example it will just pass through him. If I want to use it as a togglable platform it's very hard to stay on the platform because of the fast accelerating. I want a togglable platform so player can switch between 2 states with ingame pressure plate (or button object) .Thank you for you help though, I still may use the script somewhere and if I do, I will give you credits. Here is the screenshot from the current game Screenshot

avatar image george_vasilchenko · Jan 03, 2015 at 07:29 PM 0
Share

Do u want to toggle between opened/closed states or between off(nothing moves) and on(opens - closes, loop)? Do I see it correct, that pink cube (between red and buttonCube) will move? PS. Im still trying to figure out, quite intriguing already...

Show more comments
avatar image
0

Answer by tYn0 · Jan 04, 2015 at 11:57 PM

So I finally got home, checked the video and script. It works perfectly! All axis, collisions and everything else finally works. Big thank you! I simplified the script a little by removing "Remote" option and instead of using InitializeWorkingAxis() function I checked "Is Kinematic" in platform's rigidbody component. I also added 2 functions SwitchState to switch between states (obviously) and SetState to set the state. Button object has attached script which uses SendMessage() to call these functions. Here is the edited script and Here is the Youtube video of everything it in action:

 using UnityEngine;
 using System.Collections;
 
 public class Platform : MonoBehaviour 
 {
     public Transform startPoint;
     public Transform endPoint;
     public enum UseCase { Auto, Manual}
     public UseCase useCase;
     [SerializeField]
     private float _moveSpeed = 1;
     private bool _return;
     [SerializeField]
     private bool _switch = true;
     private Rigidbody _platformRigidBody = null;
     public Transform _platform;
     
     void Start()
     {
         _platformRigidBody = _platform.transform.rigidbody;
         _platformRigidBody.mass = 200;
     }
 
     void SwitchState()
     {
         _switch = (_switch == true) ? false : true;
     }
     
     void SetState(bool state)
     {
         _switch = state;
         
     }
 
     void FixedUpdate()
     {
         if (useCase == UseCase.Auto)
         {
             if (_platform.position == endPoint.position) _return = true;
             if (_platform.position == startPoint.position) _return = false;
             
             if (_return) _platform.position = Vector3.MoveTowards(_platform.position, startPoint.position, _moveSpeed * Time.fixedDeltaTime);
             if (!_return) _platform.position = Vector3.MoveTowards(_platform.position, endPoint.position, _moveSpeed * Time.fixedDeltaTime);
         }
         if (useCase == UseCase.Manual)
         {          
             if (_switch) _platform.position = Vector3.MoveTowards(_platform.position, startPoint.position, _moveSpeed * Time.fixedDeltaTime);
             if (!_switch) _platform.position = Vector3.MoveTowards(_platform.position, endPoint.position, _moveSpeed * Time.fixedDeltaTime);       
         }
 
     }
     void OnDrawGizmos()
     {
         Gizmos.color = Color.green;
         Gizmos.DrawWireCube(startPoint.position, transform.localScale);
         
         Gizmos.color = Color.red;
         Gizmos.DrawWireCube(endPoint.position, transform.localScale);
     }
 }


Comment
Add comment · Show 1 · 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 george_vasilchenko · Jan 05, 2015 at 08:10 AM 0
Share

Hey! I am glad it worked for you! Your level looks nice! You can always ask if you need help. You can send message directly. Good luck!

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

Multiple Cars not working 1 Answer

Toggle on click. 2 Answers

How to make my ball not moving on a curved platform? 2 Answers

Hello I have a gun swich script that doesn't work can someone help me please? 2 Answers

How to script a door opening with points...please help 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