- Home /
I want to disable character movement and camera movement while certain animation is playing
So I have animation that is playing when game is started and I want to disable Character movement and camera movement script while this animation is playing and enable it when its done. this is a character movement code.
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
this is a camera movement code.
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 180f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
Answer by rage_co · Jul 11, 2021 at 01:03 PM
just put everything in the update methods an if function...for example if you want to stop them while the game is paused you can just use....
void Update()
{
if(pauseManager.gamepause)//reference a bool
{
//your script
}
}
this hould be much taxing performance wise and should get the job done
I want to disable the movement for 3 seconds and then enable.
you can use a coroutine....create a new method but do not use void and instead use IEnumerator...these are used to specify coroutines...
first you need to make a variable pause to manage the pausing...you have to put it in the if function mentioned above....after that create a simple void method to Enable the pause...
void BeginPause()
{
pause = true;
}
you can call this when you want the pause to take effect by just calling...
BeginPause();
from within the script(make it public and reference it from another script if you want it to be called from another script). After that you need to create a coroutine like this....
IEnumerator EndPause()
{
yield return new WaitForSeconds(pauseTime); //pauseTime is the amount of time you want it to stall for
pause = false;
}
After that you need to call this coroutine from within the BeginPause() method....like this....
void BeginPause()
{
pause = true;
StartCoroutine(EndPause());
}
and there you have it, this should do it.....hope this helps
If that worked please accept the answer so that it's more feasible to people on the future