Skip to main content

Initializing non-VR player character

To initialize a non-VR player character in an existing VR project in Unity, follow these steps:

  1. 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".

  2. 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.

  3. 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.

  4. Create a child GameObject: right-click on the Player GameObject in the Hierarchy panel and select "Create Empty". Name this object "Player Controller".

  5. 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.

  6. 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 playerPlayer GameObject in the Scene view or Hierarchy panel.

  7. Set up player controls: Create a new C# script by right-clicking inAttach the ProjectFirst panelPerson and selecting "Create -> C# Script." Name it something like "PlayerController." Attach thisMovement script to the playerPlayer Controller GameObject by dragging and dropping it onto the GameObject or using the "Add Component" button. OpenModify the scriptrunning/walking/jump inspeed to your preferred code editor.

  8. Write the player control logic: Inside the PlayerController script, write the code to handle player input and movement. For example, you might use the Input class to detect key presses or touch events and then move the player character accordingly. Refer to Unity's documentation for more information on handling input and controlling character movement.

  9. Initialize the player's position and rotation: In the Start() method of your PlayerController script, you can set the initial position and rotation of the player character using the transform component of the GameObject. For example, you can use transform.position to set the starting position, and transform.rotation to set the initial rotation.liking.

  10. 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 athe newRaycaster script to the player character GameObjectGameObject. andThis writescript code to cast a ray from the player's position in the direction they are facing. Useuses 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.

  11. 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.