- Home /
How Can I Make a Moving Object with a trigger reaction ? [C#]
Hi
I'm a beginner on Unity and I want to make this
You can see that on the 1 situation, bloc A and B are continualy moving up and down,
when the player enter in the trigger those bloc move to be stick together,
and when you go out the trigger, they move to their initial place and go up and down like
the first image
how can I do this thing please
Thank you
Answer by Chronos-L · Mar 15, 2013 at 04:59 AM
Simple state machine.
//Incomplete script
public class Block : MonoBehaviour {
public string state = "moving";
void Update() {
if( state == "moving" ) {
//Do up and down movement
}
else {
//Stick to something
}
}
}
Then, attach a script to the trigger that modifies the state of the Block
when a player enter/exit the trigger.
public class TriggerZone : MonoBehavior {
public Block a, b;
void OnTriggerEnter( Collider other ) {
if( other.gameObject.CompareTag("player") ) {
a.state = "stick";
b.state = "stick";
}
}
void OnTriggerExit( Collider other ) {
if( other.gameObject.CompareTag("player") ) {
a.state = "moving";
b.state = "moving";
}
}
}
This is very rudimentary. You will have to do some work to get it to be exactly what you show in the picture. By the way, you can press the picture icon or Ctrl+G to attach a photo to your question.
Your answer
Follow this Question
Related Questions
how to make a moving object 2 Answers
How do I move an object from point A to point B using a trigger? 3 Answers
Moving an object 1 Answer
2D game object trigger 1 Answer
Move object A towards object B 2 Answers