Separate "move forward" script

It’s difficult to explain.

I am stuck in the lvl 3 of my game. It will have 5 Tutorial levels, and in this one, i want to make the following:

The player jumps in a platform, and then, he will move it manually. I want to make this using two scripts:

The script one makes the player move with A and D. (Simple c# script).

The script two that is my problem. I need to make a script that when you jump in a platform, you automatically “gain it’s control” and then you can move to right with P. How do i do that?

1 Answer

1

I would probably start by doing an on collision on the player and set a variable like:

  public bool inPlatform;

  void Start() {
      inPlatform = false;
   }

   void OnCollisionEnter2D(Collision2D other) {
        if(other.gameObject.name == "platform") {
            inPlatform = true;
        }
   }

Then in the other script:

PlayerScript player;

void Start() {
  GameObject p = GameObject.Find("PlayerGameObject");
  player = p.GetComponent<PlayerScript>();
}

void Update() {
if (player.inPlatform) {
	player.transform.position = transform.position;

	if(Input.GetKey(KeyCode.P)) {
		//Move the platform
	} else if(Input.GetKey(KeyCode.Space)) {
		//Move the player outside the collision by jumping
		player.inPlatform = false;
	}
}
}

Thanks! It worked perfectly.