Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by melvinweert · May 07, 2020 at 11:54 AM · c#2d-platformercollision2d

[HELP!] Run in opposite direction on wall collision

Hi! Im working on an 2d platformer. I want the player to run in the opposite direction once it hits a wall collider. I can't get it working.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerController : MonoBehaviour
 {
 
     public Rigidbody2D rb;
 
     public float movementSpeed = 5f;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         transform.position += transform.right * movementSpeed * Time.deltaTime;
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "wallright")
         {
             Vector2 theScale = transform.localScale;
             theScale.x *= -1;
             transform.localScale = theScale;
         }
 
         if (other.gameObject.tag == "wallleft")
         {
             Vector2 theScale = transform.localScale;
             theScale.x *= 1;
             transform.localScale = theScale;
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by wasssim · May 07, 2020 at 12:17 PM

@ItsMelvin Instead of changing the scale of your character, maybe you can multiply you speed with the opposite value of your current MovementSpeed like this:

 if (other.gameObject.tag == "wallright")
 {
               transform.position += transform.right * -movementSpeed * Time.deltaTime;
 }



Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image melvinweert · May 07, 2020 at 12:21 PM 0
Share

Hi! Thanks for your reply. I tried it but it doesn't seem to work. $$anonymous$$ight be me doing something wrong. I changed it like this:

     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "wallright")
         {
             transform.position += transform.right * -movementSpeed * Time.deltaTime;
         }
 
         if (other.gameObject.tag == "wallleft")
         {
             transform.position += transform.right * movementSpeed * Time.deltaTime;
         }
     }

 
avatar image wasssim melvinweert · May 07, 2020 at 12:31 PM 0
Share

$$anonymous$$aybe you can use raycast to slove this problem, it's a different alternative ins$$anonymous$$d of using this method

avatar image wasssim wasssim · May 07, 2020 at 12:46 PM 0
Share
 public Transform wallDetection;
 private bool movingRight = true;
 
 void update()
 {
 RaycastHit2D wallInfo = Physics2D.Raycast(wallDetection.position, Vector2.right, 2f);
 if(wallDetection.collider == false)
 {
 if(movingRight = true)
 {
 transform.eulerAngles = new Vector3(0, 180, 0);
 movingRight = false;
 }
 else
 {
 transform.eulerAngles = new Vector3 (0, 0, 0);
 movingRight = true;
 }
 }
 }
avatar image wasssim melvinweert · May 07, 2020 at 12:31 PM 0
Share

I am sending you a script in a few $$anonymous$$utes

avatar image melvinweert wasssim · May 07, 2020 at 12:34 PM 0
Share

That would be great thanks!

avatar image wasssim melvinweert · May 07, 2020 at 12:47 PM 0
Share

You have to create an empty gameObject, and then you have to put it at the right of you player, this empty gameObject is going to create a Raycast that will change the direction of you player when hitting a wall

avatar image melvinweert wasssim · May 07, 2020 at 01:01 PM 0
Share

I still didnt get it to work. Im sorry I'm not that great at c#. It gave an error at the if(walldetection.collider == false) line so I changed it idk if I did it right. $$anonymous$$y gameobject is now just spinning while standing still. Thanks in advance!

 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerController : $$anonymous$$onoBehaviour
 {
 
     public Rigidbody2D rb;
 
     public Transform wallDetection;
     private bool movingRight = true;
 
     public float movementSpeed = 5f;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
         RaycastHit2D wallInfo = Physics2D.Raycast(wallDetection.position, Vector2.right, 2f);
         if (wallDetection.GetComponent<BoxCollider2D>() == false)
         {
             if (movingRight == true)
             {
                 transform.eulerAngles = new Vector3(0, 180, 0);
                 movingRight = false;
             }
             else
             {
                 transform.eulerAngles = new Vector3(0, 0, 0);
                 movingRight = true;
             }
         }
     }
 }
Show more comments
avatar image
0

Answer by Parthon1 · May 07, 2020 at 01:14 PM

So you need to record which direction the player is going in, then reverse that.

under movespeed:

 public float playerdirection = 1; // for right

then in movement:

 transform.position += transform.right * playerdirection * movementSpeed * Time.deltaTime;

then in the colliders:

  if (other.gameObject.tag == "wallright")
  {
      playerdirection = -1;
  }

  if (other.gameObject.tag == "wallleft")
  {
      playerdirection = 1;
  }




Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image melvinweert · May 07, 2020 at 01:25 PM 0
Share

I got it working thanks for your answer!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I know if collision is from right or left when using a Tilemap Collider 2D 0 Answers

Subtract value from one script to another 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

problem Getting AI projectile to collide with player 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges