Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to change the facing direction of one object when it hit the wall?

Discussion in '2D' started by Dumitru, Jun 2, 2015.

  1. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    Helllo, so I'm making a 2D platformer and I can't seem to get the character to rotate / reverse movement as soon as he hits a wall, any ideas or help will be greatly appreciated.

    P-S : I'm new to unity and scripting, sorry if its something simple, thank you for taking your time to read this post.
     
  2. Gardes

    Gardes

    Joined:
    Apr 7, 2015
    Posts:
    46
    I dont know if there is any easier solution (i dont know what you want exactly (flip sprite or somethings like push back)). I just guessing a bit and got some pushback script for you.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test : MonoBehaviour {
    5.  
    6.     public float pushback = 50;
    7.  
    8.     Rigidbody2D rb;
    9.     Vector2 vel;
    10.  
    11.     void Start() {
    12.      
    13.         rb = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D> ();
    14.     }
    15.  
    16.     void OnTriggerEnter2D (Collider2D other) {
    17.  
    18.         vel = rb.velocity;
    19.     }
    20.  
    21.     void OnCollisionEnter2D (Collision2D col) {
    22.      
    23.         if (col.gameObject.tag == "Player"){
    24.  
    25.             rb.AddForce(-vel * pushback);
    26.         }
    27.     }
    28. }
    You have to attach a new collider to your wall and make it a trigger. The trigger should be a little bigger then your true collider. If your collider is scaled at 1, I figured out the trigger should be scaled to 1.05 for good results.
     
    Dumitru likes this.
  3. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    Thank you so much! to make things more clear, the character sprite must turn in his opposite direction as soon as he hits the wall that he is facing on the x positions, not y. On top of that, I have no idea on where to put this script in the controller player because I have a jumping script inside it, tried before with another script and it broke my character controller script so now I have to restart from scratch >.>

    Also , I have clicked on the " is trigger " in the Box Collider 2D but I cannot chose the size of the collider to make it 1.05 instead of 1, how can I do that?

    Also, one more question, my character always moves forward, how could I add in the script, a script that when a key is pressed, the character flips, that way the player has more controll over the character
     
    Last edited: Jun 2, 2015