Question by
BeanSlice · May 10, 2017 at 03:10 PM ·
c#freezepositionfreezerotation
Freeze players position and rotation for a time?
I have written a jumpscare script in C# to show and hide an image for a certain amount of time. This image is in front of the player but not too far away (silhouette of monster), so while the jumpscare is happening, I don't want the player to be able to keep walking towards the image - I want to freeze their position and rotation. Here is my code, everything works and runs but the player is not frozen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShowMonster : MonoBehaviour
{
public GameObject monster;
public float time = 0.5f;
public float flashlightOffTime = 1.0f;
public AudioClip scareSound;
public Light flashlight;
public Rigidbody rigidbody;
public float jumpscareEnd = 4.0f;
public KeyCode keyCode;
private RigidbodyConstraints originalConstraints;
AudioSource sound;
void Start ()
{
originalConstraints = rigidbody.constraints;
rigidbody = rigidbody.GetComponent<Rigidbody> ();
flashlight = flashlight.GetComponent<Light> ();
sound = GetComponent<AudioSource> ();
monster.GetComponent<Renderer> ().enabled = false;
flashlight.intensity = 1;
}
void OnTriggerEnter (Collider other)
{
StartCoroutine (MonsterScare());
if (Input.GetKeyDown (keyCode))
{
flashlight.enabled = false;
}
}
IEnumerator MonsterScare()
{
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX;
flashlight.intensity = 0;
yield return new WaitForSeconds (flashlightOffTime);
flashlight.intensity = 1;
sound.PlayOneShot(scareSound);
monster.GetComponent<Renderer> ().enabled = true;
yield return new WaitForSeconds (time);
monster.GetComponent<Renderer> ().enabled = false;
rigidbody.constraints = originalConstraints;
yield return new WaitForSeconds (jumpscareEnd);
JumpScareEnd ();
}
void JumpScareEnd ()
{
Destroy (monster);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to freeze an object when collision? 4 Answers
Freeze Rigidbody 1 Answer
Enabling Freeze Rotation changes rotation axis 0 Answers
Camera Lock/freeze on keypress 0 Answers
Camera Lock/freeze on keypress 1 Answer