Physics2D.OverlapCircle working in Unity but not in build
Hello,
Making my first game here and I wanted grid based movement for it, I based my code from this video
if (rb.rotation == 0f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, whatStopsMovement) && // outlining character
!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal")+ 1f, 0f, 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f); //moving
}
}
This checks the outline of the player in both relevant directions based on the player rotation, the player is a two unit wide rectangle for now.
This code works fine in the editor, however once rotated in the build it stops moving or rotating at all. I have troubleshot it a fair bit and when I remove the Physics2D.OverlapCircle I can move freely in the built game, but this doesn't represent how the character should move. I tried to change the physics settings in the original Unity project and this problem persisted but I could have done that incorrectly.
Overall I am just really curious as why this is working in the editor but not the build. Full script to follow.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NotWorking : MonoBehaviour
{
public Rigidbody2D rb;
public Transform movePoint;
public LayerMask whatStopsMovement;
private float moveSpeed = 0.1f;
private Vector3 movePointPosition;
bool rotateSuccess = true;
Vector2 movement;
bool isRotating = false;
void Start()
{
movePoint.parent = null;
movePointPosition = movePoint.position;
}
void Update()
{
if (Vector3.Distance(transform.position, movePoint.position) == 0f)
{
//seeing valid movement options
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1)
{
if (rb.rotation == 0f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, whatStopsMovement) && // outlining character
!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal")+ 1f, 0f, 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f); //moving
}
}
else if (rb.rotation == 90f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, whatStopsMovement)&&
!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 1f, 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
}
}
else if (rb.rotation == 180f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, whatStopsMovement)&&
!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal") - 1f, 0f, 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
}
}
else if (rb.rotation == 270f || rb.rotation == -90f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, whatStopsMovement)&&
!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), -1f, 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
}
}
}
//seeing valid movement options
else if (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1)
{
if (rb.rotation == 0f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement) &&
!Physics2D.OverlapCircle(movePoint.position + new Vector3(1f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
}
}
else if (rb.rotation == 90f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement) &&
!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical") + 1f, 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
}
}
else if (rb.rotation == 180f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement) &&
!Physics2D.OverlapCircle(movePoint.position + new Vector3(-1f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
}
}
else if (rb.rotation == 270f || rb.rotation ==-90f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, whatStopsMovement) &&
!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical") - 1f, 0f), .2f, whatStopsMovement))
{
movePointPosition += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
}
}
}
if (isRotating == false)
isRotating = Input.GetButtonDown("Rotate");
}
}
void FixedUpdate()
{
//actually moving player
movePoint.position = movePointPosition;
transform.position = Vector3.MoveTowards(transform.position, movePoint.position, moveSpeed * Time.deltaTime);
if (isRotating)
{
rb.rotation += 90f;
isRotating = false;
if (rb.rotation == 360f)
{ rb.rotation = 0; }
}
}
}
Your answer
Follow this Question
Related Questions
Script to random set active a child object? 0 Answers
2d Sprite creation at 0,0,0 with camera at 0,0,0 but renders wayyy off screen 0 Answers
How to make a random object generator that responds to simple touch? 0 Answers
tagged objects problem 0 Answers
Problem with array and coroutine...Sometimes Works!!! 1 Answer