im creating a game in unity for school, and need some help with a script error
so the purpose of the script is to freeze the character in place and block all inputs when "p" is pressed, and unfreezed when "p" is pressed again, its in a 3d first person unity game, and the intent is to freeze the character whereever they are, even blocking gravity
4 Replies
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FreezeCharacter : MonoBehaviour
{
private bool isFrozen = false;
private Rigidbody characterRigidbody;
private YourCharacterMovementScript movementScript;
void Start()
{
characterRigidbody = GetComponent<Rigidbody>();
movementScript = GetComponent<YourCharacterMovementScript>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
ToggleFreeze();
}
}
void ToggleFreeze()
{
isFrozen = !isFrozen;
if (isFrozen)
{
FreezeCharacterInPlace();
}
else
{
UnfreezeCharacter();
}
}
void FreezeCharacterInPlace()
{
if (movementScript != null)
{
movementScript.enabled = false;
}
if (characterRigidbody != null)
{
characterRigidbody.velocity = Vector3.zero;
characterRigidbody.angularVelocity = Vector3.zero;
characterRigidbody.isKinematic = true;
}
}
void UnfreezeCharacter()
{
if (movementScript != null)
{
movementScript.enabled = true;
}
if (characterRigidbody != null)
{
characterRigidbody.isKinematic = false;
}
}
}
my code so farTo post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/Also please read this
what isnt working?
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)