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. Dismiss Notice

Question How to Re-Center First Person starter asset?

Discussion in 'Scripting' started by riasillo, Sep 6, 2023.

  1. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    24
    When scene loads on webgl, mouse movement down rotates camera to ground and is disorienting.
    Must click in window to lock mouse. can infinitely rotate in any direction. works fine but is confusing for a new site visitor. Trying to make bullet proof user experience.

    To fix disorientation on scene load, I created a menu scene with a button that loads new scene on click and hides mouse. New scene loads with mouse hidden and centered. worked great as intended, however when mouse exits game window, it appears and goes to edge of screen and hits a max rotation not allowing infinite rotation.

    Locked is selected on the starter asset input script. Not sure how it is exiting window. Also tried editing code with confined. Did not work.

    I deleted intro scene and tried a button in main scene. PlayerFollowCamera turned off at start and setactive upon click. But Button won't click to set active again. Events has input controller.

    Same method I used in main menu to activate button.

    Controls work, despite PlayerFollow being inactive, camera just doesn't move but capsule moves and jumps.
    Clicking hides mouse due to default settings in input script. not sure why button won't click?

    Any suggestions on that or better way to load a scene without it rotating until mouse is centered?

    or a better script solution here for what I'm trying to do?

    Code (CSharp):
    1. public class StartCamera : MonoBehaviour
    2. {
    3.     public GameObject PlayerFollowCamera;
    4.      
    5.     void Start()
    6.     {
    7.      
    8.         PlayerFollowCamera.SetActive(false);
    9.  
    10.      }
    11.  
    12.     public void PlayButton()
    13.  
    14.     {
    15.  
    16.         PlayerFollowCamera.SetActive(true);
    17.  
    18.     }
    19. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Usually simply inhibiting the mouse aim for a few frames will take care of things.

    Code (csharp):
    1. private int frameCounter;
    2.  
    3. void UpdateMouseLook()   // or whatever you call this block of code
    4. {
    5.   if (frameCounter < 3)
    6.   {
    7.     frameCounter++;
    8.     return;
    9.   }
    10.  
    11.   // now process your normal mouse look here
    12. }
    You might even want to reset that whenever a game menu has to pop up.
     
  3. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    24
    gotchya, Thanks, I'll have to try that next time. seems much simpler than the hokey thing I ended up doing...

    I ended up setting the playerfollowcamera and player capsule to inactive. created a button To start game, on click activates player camera and player capsule, spawns one more text that tells user to left click to hide mouse, that left click destroys text and mouse is now locked.

    In editor I didn't need that extra left click to focus, but in webgl mouse was not locked in? Don't understand why.
    Figuring it might have something to do with application being in focus in editor and not webgl?

    I deleted the native bool in the 3rd person controller for the locked mouse OnApplicationFocus and set the locked cursor state on void start, figuring when it was activated that would lock in then. (not sure if void start is just for start of game or start of a gameObject script when it is activated, but it worked?)

    little bit of a hokey workaround but I'm calling it good enough, since I don't really know anything about coding, which you can probably tell by my questions and work.

    Should be pretty bullet proof for first time users of application though, even if not best flow.

    I created a 3d tour of equipment I sell, to give users an interactive visual. Lot of work but pretty pleased with outcome after a few months starting from 0 knowledge with unity and blender.

    Appreciate your responding to my questions.