1
\$\begingroup\$

I have a box collider and right now when I walk into it it says "you can't go there yet" I would like it for it to also move the player like 3 steps to the right or something. How would I do thatr?

\$\endgroup\$
0
\$\begingroup\$

You have not provided enough info, but I may have a solution.

You may have a void Player::playAnimation function in your 'player.cs' file for your PLAYER gameObject.

Go to the file where your controls for the player are. Read and understand the code. It should have this:

If (Input.GetKey(KeyCode.Right) == true) {
  player.moveRight;
}

Now, moveRight is a function that moves the player right. This is perhaps the code for it:

void Player::moveRight() {
  animator.SetBool("MoveRight", true);
  GameObject.move(Vector3.right);
  _facing = RIGHT;
}

Now, you can simply apply this playAnimation logic here in your box gameObject (tagged here as 'box1'):

void OnCollisionEnter2D (Collision2D coll) { 
  string tag = coll.collider.gameObject.tag; 
  if (tag == "box1") { 
    player.moveRight; 
    } 
  }

Hope that helped.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.