- Home /
[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;
}
}
}
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;
}
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;
}
}
$$anonymous$$aybe you can use raycast to slove this problem, it's a different alternative ins$$anonymous$$d of using this method
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;
}
}
}
I am sending you a script in a few $$anonymous$$utes
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
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;
}
}
}
}
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;
}