- Home /
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);
}
}
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:
Work automatically (just moving back and forth with no interaction needed from player);
Work manually (you simply click a mouse button to open or close the door/platform);
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:
Player object with collider and rigidbody attached and mass set to something small (1-10);
Platform object (no components needed, they will appear automatically);
startPoint object, just transform;
endPoint object, just transform;
button object with BoxCollider attached and aligned desired way;
After you dragged and dropped the script onto your "platform" game object:
assign "startPoint" object to the field "Start Point";
assign "endPoint" object to the field "End Point";
choose desired axis of interaction from the dropdown menu "Used Axis";
choose desired behavior from the dropdown menu "Use Case";
assign your "button" object to the field "Button";
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;
}
}
}
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
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..
$$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
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...
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);
}
}
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!