- Home /
Player acts differently if not selected in the inspector
Hi all, I hope someone could help me. I'm making a roll-a-ball game involving forces, speed and timescale. I notice a weird behaviour while playing: IF the player game object is selected in the inspector, everything works correctly as I intended in my scripts. IF NOT the player acts very differently, the applied force is weaker (so that I cannot reach the max speed), the "spin" value I created, which should decrease by 0.1 on each click, depleates with a different speed, the regeneration of the same value is faster then normal etc... Maybe it's something related to the new prefabs system of some mistake I made manipulating the timescale (and the related fixed delta time). Please help! Here's the script:
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;
class PHOTON : MonoBehaviour
{
public Rigidbody PhotonRB;
public float pushForce;
public float spinMax = 1;
public float spin = 1;
public float spinRegRate = 0.1f;
private Plane plane = new Plane(Vector3.up, Vector3.zero);
public AudioSource moveSound;
public Light photonLight;
public bool isLightSpeed = false;
private void Start()
{
moveSound = GetComponent<AudioSource>();
StartCoroutine(regenerateSpin());
spin = spinMax;
spinRegRate = PlayerPrefs.GetInt("PlayerNewLevel");
}
public void LevelUp()
{
spinRegRate = PlayerPrefs.GetInt("PlayerNewLevel");
}
private void Update()
{
if (transform.position.y >= 5)
{
transform.position = new Vector3(transform.position.x, 0, transform.position.z);
}
if (Input.GetMouseButtonDown(0) && isLightSpeed == false && spin >= 0.1f)
{
decreaseSpin(0.1f);
}
if (Input.GetMouseButtonDown(0) && isLightSpeed == true)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f))
if (hit.collider != null)
{
transform.position = new Vector3(hit.point.x, 0.5f, hit.point.z);
Debug.Log("hit" + hit.collider.name);
}
}
else
{
return;
}
}
void decreaseSpin(float amount)
{
spin -= amount;
if (spin <= 0)
{
spin = 0;
}
}
IEnumerator regenerateSpin()
{
while (true)
{
if (spin < 1)
{
spin += spinRegRate * 0.001f;
yield return new WaitForSeconds(0.01f);
}
else
{
yield return null;
}
}
}
IEnumerator lightSpeed()
{
isLightSpeed = true;
Time.timeScale = 0.05f;
PhotonRB.isKinematic = true;
yield return new WaitForSeconds(0.5f);
isLightSpeed = false;
Time.timeScale = 1f;
PhotonRB.isKinematic = false;
//LightSpeedSlider.gameObject.SetActive(false);
}
private void FixedUpdate()
{
photonLight.intensity = Mathf.Lerp(1f, 30f, PhotonRB.velocity.magnitude/100);
// photonLight.range = (PhotonRB.velocity.magnitude);
PhotonRB.mass = Mathf.Lerp(1f, 0.01f, PhotonRB.velocity.magnitude / 100);
PhotonRB.drag = Mathf.Lerp(0f, 1f, PhotonRB.velocity.magnitude / 1000);
if (PhotonRB.velocity.magnitude >= 100f)
{
StartCoroutine(lightSpeed());
}
if (Input.GetMouseButtonDown(0) && isLightSpeed == false)
{
moveSound.Play();
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float enter;
if (plane.Raycast(ray, out enter))
{
var hitPoint = ray.GetPoint(enter);
var mouseDir = hitPoint - gameObject.transform.position;
mouseDir = mouseDir.normalized;
PhotonRB.AddForce(mouseDir * pushForce);
}
}
}
}
I am having the same problem with Unity 2019.3.11
A player object with Rigidbody2D and Collider2D checks if it is grounded via getting its collider's contact points. Jumping depends on being grounded first. When standing on a vertically moving platform, the grounded bool of the player object unpredictably shifts between true and false when the platform is moving (this happens in a build of the game too). If the object is selected in the inspector, this no longer happens and the grounded bool, along with its jump, work as expected.
Any chance you might have figured out the cause of this strange behavior?
Same problem here, but with Scriptable Object. A Bool doesn't change from true to false if the scriptable object isn't selected to be viewed in inspector.
Answer by Kahun4 · Apr 29, 2020 at 11:18 AM
I had the same sort of problem and solved it by moving the methods that handled movement of the rigidbody of the player from Update to FixedUpdate.
Answer by unity_wjfOb90B6Ao9WA · Dec 19, 2021 at 10:10 PM
I'm facing the same issues. In my case, there's a player ( downloaded from a prefab ) that is standing on a platform ( platform is its parent ), and the platform is based on AR, so it appears on a QR code printed on a paper, and moves if the paper moves. The player is always offsetted, not following at all the platform and it feels like he's sliding. But when focusing with the inspector, everything becomes fine. Any updates on this ?
Answer by WorldFlickers · Dec 28, 2021 at 07:32 PM
I got similar problem. I had a swinging anchor attached to a cube with rigidbody. And when I selected the cube in scene hierarchy, swinging became faster. I limited app fps with 30, and difference disappeared. Seems like app in editor works slower, with less fps, when a rigidbody is selected in scene hierarchy. And it means that your behaviour depends on fps.
Answer by rigidbodyinteractive · Dec 30, 2021 at 04:22 AM
I filed a bug report to Unity about this issue. They said it was a confirmed bug and it will be fixed in future releases.
Although I didn't notice any improvement in 2020.3.16.f version (i did not upgrade to the latest one yet).
What version are you guys using?
Your answer
![](https://koobas.hobune.stream/wayback/20220612210816im_/https://answers.unity.com/themes/thub/images/avi.jpg)