Initializing non-VR player character
To initialize a non-VR player character in an existing VR project in Unity, follow these steps:
-
Create a new GameObject: In the Unity Editor, right-click in the Hierarchy panel and select "Create Empty." This will create a new empty GameObject. Name this object "Player".
-
Attach a Rigidbody: Select the Player GameObject, then click on the "Add Component" button in the Inspector panel. Search for "Rigidbody" and add it to the GameObject. Set the "Interpolate" property to "None". The Rigidbody controller will handle physics for your player object.
-
Attach an Input Action Manager: Select the Player GameObject, then click on the "Add Component" button in the Inspector panel. Search for "Input Action Manager" and add it to the GameObject. Assign the "MouseControls" asset to the Action Assets.
-
Create a child GameObject: right-click on the Player GameObject in the Hierarchy panel and select "Create Empty". Name this object "Player Controller".
-
Attach a character controller: Select the newly created GameObject, then click on the "Add Component" button in the Inspector panel. Search for "Character Controller" and add it to the GameObject. The Character Controller component will handle the player's movement and collision.
-
Add a 3D model or sprite: If you want a visual representation for your player character, you can add a 3D model or sprite. Import your desired model or sprite into Unity by dragging it into the Project panel. Then, drag and drop the model or sprite onto your Player GameObject in the Scene view or Hierarchy panel.
-
Set up player controls: Attach the First Person Movement script to the Player Controller GameObject by dragging and dropping it onto the GameObject or using the "Add Component" button. Modify the running/walking/jump speed to your liking.
-
Add a raycast for UI interaction: To enable the player character to interact with UI elements such as buttons or menus, you'll need to add a raycast. Attach the Raycaster script to the player character GameObject. This script uses the Physics.Raycast method to detect if the ray hits any UI elements and perform the desired actions accordingly, such as button clicks or menu selections.
-
Test and refine: Save your script and go back to the Unity Editor. Disable the Xr Origin object to make sure that the game won't start in VR mode. Press the Play button to enter Play mode and test your player character initialization and movement. Make any necessary adjustments to the code or settings until you are satisfied with the result.
That's it! By following these steps, you can initialize a player character in Unity and have them ready for movement and interaction in your game.