transform.position = new Vector3(transform.position.x,45.6f,transform.position.z);
if i write this code, then want to unlock the y coordinate in an If statement, how do i go about doing it ?
transform.position = new Vector3(transform.position.x,45.6f,transform.position.z);
if i write this code, then want to unlock the y coordinate in an If statement, how do i go about doing it ?
You mean like:
if(someBoolean)
transform.position = new Vector3(transform.position.x, 45.6f, transform.position.z);
?
I think I may be misunderstanding your question. If you only want the y value to be locked to a specific number in the case that a boolean value is on or off or w/e, then the above code should work fine. If you mean something else, then I’m not following.
i understand what you are saying but i have this problem. When i am in water, and go to the surface which is y = 45.6f, i want to lock that position since if i move a certain way, i walk into the air.
So i want to lock that position until i press a button.
if(Input.GetKey(“q”))
{
// I AM ABLE TO MOVE THE Y AGAIN HERE
}
You mean like this?
private bool locked = false;
public void SomeFunction()
{
if(Input.GetKey("q"))
locked = false;
if(!locked && transform.position.y > 45.6f)
locked = true;
if(locked)
transform.position = new Vector3(transform.position.x, 45.6f, transform.position.z);
}
You should probably also use a second boolean value like “in water” to determine if you’re maneuvering yourself out of the water onto land, or else re-diving, when the “lock” breaks. Something like that…
Im having a stupid problem which I cant solve. When i use that correct code, and i hit the 45.6 > y , instead of locking the y, it teleports me into the air and when i land at 45.6, i teleport again into the air, idk why
else
{
inWater = true;
//rigidbody.isKinematic = false;
chMotor.movement.gravity = 2;
chMotor.movement.maxFallSpeed = 4;
chMotor.movement.maxForwardSpeed = 4;
chMotor.movement.maxSidewaysSpeed = 4;
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
chMotor.movement.maxForwardSpeed = 7;
chMotor.movement.maxSidewaysSpeed = 7;
staminaInfo.staminaBar.fillAmount -= 0.001f;
}
else
{
chMotor.movement.maxForwardSpeed = 4;
chMotor.movement.maxSidewaysSpeed = 4;
}
if(Input.GetKey("q"))
{
locked = false;
rigidbody.AddForce(Vector3.down * 5, ForceMode.VelocityChange);
if(Physics.Raycast(transform.position, down, out hit,3))
{
if(hit.collider.gameObject.tag == "ground")
{
rigidbody.isKinematic = true;
}
}
}
else
{
rigidbody.isKinematic = false;
}
}
if(inWater == true)
{
chMotor.jumping.enabled = false;
if(transform.position.y < 45.6)
{
if(Input.GetKey("space"))
{
rigidbody.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
}
}
if(!locked && transform.position.y > 45.6f)
{
locked = true;
}
if(locked)
{
transform.position = new Vector3(transform.position.x, 45.6f, transform.position.z);
}
}