I’m trying to code in a script in my Unity3D game to have my Player lock in position until the timer ends, I’m really bad at coding so I’ve already tried looking for fixes but none seem to work, How do I do this properly without getting any errors like Cannot modify the return value of ‘Transform.postion’ because it is not a variable? (Also this script is attached to my Player)
The code for the script is `using UnityEngine;
using System.Collections;
public class SimpleTimer : MonoBehaviour
{
public float targetTime = 22.0f;
public void Update()
{
targetTime -= Time.deltaTime;
if (targetTime <= 0.0f)
{
timerEnded();
}
if (targetTime <= 22.0f)
{
transform.position.x = 0.0f;
transform.position.y = 0.0f;
transform.position.z = 0.0f;
}
}
void timerEnded()
{
transform.position.x = 1.0f;
transform.position.y = 1.0f;
transform.position.z = 1.0f;
}
}
`
I probably messed up this code so hard… ♂️
Instead of
transform.position.x = 1.0f;
transform.position.y = 1.0f;
transform.position.z = 1.0f;
use transform.position = Vector3.one;
or Vector3.zero
Does Vector3.one mean It’s not locked and does Vector3.zero mean it is locked?
Vector3.zero is the same thing as new Vector3(0, 0, 0)
and Vector3.one is the same as new Vector3(1, 1, 1)
Your compile error though is because you have to replace the whole position at once, you can’t just change one of the values like that.
PraetorBlue:
Vector3.zero is the same thing as new Vector3(0, 0, 0)
and Vector3.one is the same as new Vector3(1, 1, 1)
Your compile error though is because you have to replace the whole position at once, you can’t just change one of the values like that.
Okay, Well I’m wanting unlock the postion when the timer ends, How do I do that?
Cause doing transform.position = Vector3.zero; just… well, Locks it in the zero position.
Does your player have a Rigidbody attached?
Try GetComponent<Rigidbody>.constraints = RigidbodyConstraints.FreezeAll
and GetComponent<Rigidbody>.constraints = RigidbodyConstraints.None
using UnityEngine;
using System.Collections;
public class SimpleTimer : MonoBehaviour
{
public float targetTime = 22.0f;
public void Update()
{
targetTime -= Time.deltaTime;
if (targetTime <= 0.0f)
{
timerEnded();
}
if (targetTime <= 22.0f)
{
GetComponent<Rigidbody>.constraints = RigidbodyConstraints.FreezeAll;
}
}
void timerEnded()
{
GetComponent<Rigidbody>.constraints = RigidbodyConstraints.None;
}
}
that my code now and I’m getting errors
Sorry I made a typo:
Try GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll
and GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None
Thanks, But the timer doesn’t work which sucks… Thanks so much tho!