Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Trying to freeze FPS character to one point for a specific amount of time

Discussion in 'Scripting' started by Code1345, May 26, 2018.

  1. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Hello! So, I have a problem that has been driving me crazy. I am trying to create a simple scene where the player enters a dungeon, and is frozen in place looking at the enemy speak for about five seconds. Afterwards, the player would be unlocked from any restraints. I have tried using the freeze rigidbody method, but the player would just run in place. The script I have is also on the FPS controller, and it has a sword and shield attached, so I can't disable it.Using this script here will partially freeze the player, letting them sprint and walk in place. Also, when the five seconds is up, the camera will jerk wildly for a few seconds before going off to the right, and everything returns to normal. Any help would be greatly appreciated!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class FPSplacement : MonoBehaviour {
    8.      
    9.     private GUIruntime2 guiruntime2;
    10.     public GameObject FPSController;
    11.     public Transform target;  
    12.     public GameObject spawn;
    13.     // Use this for initialization
    14.     void Start () {
    15.         Scene currentscene = SceneManager.GetActiveScene();
    16.         string sceneName = currentscene.name;
    17.         guiruntime2 = FPSController.GetComponent<GUIruntime2>();
    18.  
    19.  
    20.  
    21.  
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("SimpleSword2part2") && guiruntime2.roar == true)
    28.         {
    29.             StartCoroutine(Waiting());
    30.            
    31.         }
    32.     }
    33.     IEnumerator Waiting()
    34.     {
    35.        
    36.      transform.position = spawn.transform.position;
    37.         //transform.LookAt(target); is to make the player look at the NPC.
    38.         transform.LookAt(target);
    39.         //target is the enemy
    40.         yield return new WaitForSeconds(5);
    41.         //To make the character look at the NPC after the timer.
    42.         transform.LookAt(target);
    43.         //guiruntime2 is the dialouge that the NPC has on screen. After five seconds it becomes disabled.
    44.         //when guiruntime is finished, it has a bool that is set to false.
    45.         guiruntime2.roar = false;
    46.      
    47.  
    48.  
    49.  
    50.  
    51.     }
    52. }
    53.  
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I think you've drastically overcomplicated this and that's probably why you're having problems. What you could do instead of messing with the rigidbody is simply SetActive ( false ) on the FPSController script itself and then set it to true. This is a very common method that quickly does what you're looking for this will disable the movement but should keep all the physics normal and then you can move again when active is set to true.

    Since your character won't be able to move or attack anyway and there will be dialogue, I really don't see why you'd want to have the sword and shield showing to begin with. Is there a special reason?
     
  3. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    I feel like you're right in with me over complicating it, hah. And just to clarify, you mean setting the rigidbody to false right? Also, with the sword and shield, after the dialogue is over, the AI will attack, and the player will be able to fight using the sword and shield, so I wanted to have them being shown before the fight actually started.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    He was saying you could disable the script that moves the player (FPS Controller). Re-enable it after your wait time so the player can move.
     
  5. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    That worked perfectly! Now I just need to get the camera to look at the enemy once the dialogue ends :p
     
  6. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    I ended up disabling the FPS script, and it worked perfectly! Thanks so much !
     
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    No problem, glad you got it sorted, it's a very common trick people use to get around that type of thing. In order to enter and exit vehicles you disable scripts and so on too so that the FPSController and a Vehicle Controller won't conflict with each other, no need for anything fancy.
     
    Code1345 likes this.