Hi,
Hope everyone is doing great?
I have a weird issue where a raycast (I assume since I have no errors to determine) is working in the editor, but not in the build.
I have a player who needs to be able to push crates around. In order to make the crates feel realistic, when the player is not pushing them, they have a heavy mass and gravity. When the player is pushing them, the mass and gravity decreases.
Both the player and crate have a rigidbody2d and box collider with isTrigger = false.
This is what I have on the player controller Update method:
Physics2D.queriesStartInColliders = false;
RaycastHit2D hit = Physics2D.Raycast(_interractableCheck.position, Vector2.right * _facingDirection, _interractableCheckDistance, _whatIsInterractable);
if (hit.collider) {
_isPushing = true;
_speed = _pushSpeed;
} else {
_isPushing = false;
_speed = _moveSpeed;
I have a crate controller script attached to the crate:
using UnityEngine;
public class CrateController : MonoBehaviour
{
private Rigidbody2D _rigidbody2D;
private PlayerController _player;
private float _originalMass;
private float _originalGravityScale;
[SerializeField] private float _newMass;
[SerializeField] private float _newGravityScale;
private void Awake() {
_rigidbody2D = GetComponent<Rigidbody2D>();
_player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
}
private void Start() {
_originalMass = _rigidbody2D.mass;
_originalGravityScale = _rigidbody2D.gravityScale;
}
private void Update() {
if (_player._isPushing) {
_rigidbody2D.mass = _newMass;
_rigidbody2D.gravityScale = _newGravityScale;
} else {
_rigidbody2D.mass = _originalMass;
_rigidbody2D.gravityScale = _originalGravityScale;
}
}
}
This works perfectly in the editor, but as soon as I do a build, the player is unable to move the crates.
Is there something I am missing? Is there a better way to do this?
Many Thanks,
J