There will be a setting in the Inspector panel to set the rotating speed. Why is Player rotating in SetupState
? It allows you to view the changes made to Player. It's also a nice visual effect, much better than just looking at a static screen.
PlayerControl
.setupSpinSpeed
variable as shown on line 6 of the preceding screenshot.Update()
as shown on line 13 of the preceding screenshot.To control the rotation of Player, we need to add the code (shown in the following screenshot) to SetupState
.
The SetupState
class needs to have two pieces of information to be able to control Player.
PlayerControl
Component script attached to PlayerTherefore we need two variables to store this information in SetupState
. Then it's just a matter of using Dot Syntax to control the Player.
player
as shown on line 10.controller
as shown on line 11.SetupState()
constructor method, add lines 19 and 20.StateUpdate()
method, add lines 25 and 26.Let us analyze the code shown in the preceding screenshot:
In PlayerControl
:
Line 6: public float setupSpinSpeed = 50.0f;
public
so it will appear in the InspectorLine 13: public void PlayerUpdate ()
In SetupState
:
Line 10: private GameObject player;
player
is going to store a reference to the Player GameObjectprivate
because only SetupState
needs to see this variableLine 11: private PlayerControl controller;
controller
is going to store a reference to the PlayerControl
Component object that's attached to PlayerLine 19: player = GameObject.Find("Player");
GameObject.Find()
is the method used to find the Player GameObjectplayer
variableLine 20: controller = player.GetComponent<PlayerControl>();
GetComponent()
method gets the PlayerControl
script Component that's attached to Player and stores a reference of PlayerControl
in the controller
variablepublic
variables and methods in the PlayerControl
scriptLine 26: controller.transform.Rotate(0, controller.setupSpinSpeed * Time.deltaTime, 0);
Rotate()
method causes the Player GameObject to spinRotate()
is a method available in the Transform
class and it takes a Vector3
parameterx
, y
, and z
transform
is a variable that stores a reference to the PlayerControl
Transform Component object in memoryx
and z
axes are 0
because we only want Player to spin around the vertical y axiscontroller.setupSpinSpeed
statement retrieves the value stored, 50.0
, in the setupSpinSpeed
variable of the PlayerControl
scriptTime.deltaTime
to give a rotation speed of 50
degrees per secondLine 25: if(!Input.GetButton("Jump"))
if
statement checks to see if the Space bar is not being pressedJump
and you will see that Jump
is mapped to the Space bar3.135.188.121