I have solved the issue myself. Elaboration in comments.
Attempt to unfreeze constraints using RigidbodyConstraints.None has no effect
Hello I am having trouble unfreezing the constraints of a gameobject in the update method after setting it's constraint(YPosition) in the start method.
By adding a debug.log line in the update method, I can see the log showing up in the console when the spacebar key was being pressed in Play mode. However the ballRb.constraints = RigidbodyConstraints.None didn't seem to be triggered/working.
Edit: I've also tried using this line
ballRb.constraints &= ~RigidbodyConstraints.FreezePositionY;
suggested in another unity answer but to no avail.
Any help or suggestion will be much appreciated.
Code attached below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject ball;
private Rigidbody ballRb;
private Vector3 spawnLocation = new Vector3(0,2.6f,-12);
void Start()
{
ballRb = ball.GetComponent<Rigidbody>();
ballRb.constraints = RigidbodyConstraints.FreezePositionY;
ball.transform.position = spawnLocation;
Instantiate(ball);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Start");
ballRb.constraints = RigidbodyConstraints.None;
Debug.Log("End");
}
}
}
I have solved this issue by simply revising the documentation. All I needed to do was to create a separate script with this code and attach it to the prefab ball itself instead of writing inside the playercontroller script attached to the player/main camera. Hope this will be helpful to anyone in the future that stumbles upon issues like this one.