Verificado com a versão: 2017.4
This category of scripts is all about moving GameObjects around, whether it is the player, hazards, or non-playing characters.
Being the Playground all based on physics, they almost all require a Rigidbody2D to produce movement, and potentially some type of Collider2D if you want the object to be able to interact with others.
AutoMove applies a continuous force to GameObject. Useful for things such as rockets, arrows, and other self propelled objects.
The direction is expressed through a Vector2 and includes the strength, and it can be absolute or relative to the object's rotation.
In the Scene view, a green arrow gizmo represents the direction of the push, while its size represents the strength.
|Note: If you are considering using this script on an prefab created with the help of the Object Shooter script, remember that Object Shooter already applies a force to objects when shooting them. In case of a non self-propelled object (like a catapult rock), you don't need Auto Move on the projectile.|
Requires: Rigidbody 2D
AutoRotation applies a continuous rotation to a GameObject on the Z axis. It can be used to add movement to a decorative object, but also to create rotating obstacles in conjunction with a ConditionCollision script. You can specify the speed, and setting a negative one makes the object rotate counter-clockwise.
In the Scene view, a green arrow gizmo represents the direction of the rotation.
Use CameraFollow on a GameObject that has a Camera component. This is useful for action adventure games where the camera is centred on the Player. Assign a moving GameObject in the scene as the Target.
|Note: Add this script to the Camera, not to the object being followed!|
If you tick the property Use Bounds, you will be able to constrain the movement of the Camera to a rectangle.
You can adjust the values of the bounds through the Inspector or in the Scene View with the yellow rectangle gizmo:
FollowTarget forces the GameObject to follow a specified target indefinitely.
The Look at target option allows to select if the object orients itself to look at the target. See the Move script for more information on it.
|Tip: You can assign this script to an enemy and use the player as a target to create a constant threat, or you can create a queue of characters by chaining them to each other with a series of Follow Target scripts.|
Requires: Rigidbody2D (and a Collider to be able to land!)
A simple script that propels an object upwards when a specific key is pressed, useful to create a jump behaviour. The Key property is the keyboard key that is used to jump.
To stop the player from jumping in the air, check Check ground and select a tag. Then, you need to tag anything you want to consider ground with that tag. As soon as the GameObject collides with the "ground", it is able to jump again.
If Check ground is not checked, the character is able to jump multiple times in the air. That might be useful to create a wing flap more than a jump.
|Tip: You might want to tune the Jump Strength property together with the Friction property of the Rigidbody2D, to obtain exactly the jump dynamics that you want.|
This scripts applies a constant force to the GameObject on two axes, while pressing either the Arrow keys or WASD.
The Type of Control property assigns which control scheme to use. You can have two of these scripts in the scene and assign one to each player, to create multiplayer games played on the same keyboard.
The Movement Type property allows to restrict the movement on only one axis. You can think of it as if it’s moving on a railing, but you can also combine it with other movement scripts to create more refined movement. For instance, you can create a platformer controller by using this script and forcing it to the horizontal axis, in conjunction with a Jump script.
|Note: Keep in mind that even if the force you apply is on one axis, nothing is stopping the object from moving on the other axis as a result of a collision, so if this GameObject is hit by another one that might disrupt the gameplay. To account for it, check the Freeze Position option for the appropriate axis on the Rigidbody2D.|
Orientation gives you control over whether the object should rotate to face the direction of travel. This is useful for vehicles (spaceships, cars, boats, etc) and in general Sprites that are seen from the top.
If it is enabled, you can choose which side is used as the forward direction with the Use Side property. This depends on how the Sprite has been drawn.
In the image above, for instance, you would set it to Up for the spaceship, to Down for the fish, while you would leave Orientation off for the bee because it’s framed from the side and not from the top, so rotating the Sprite would look strange.
The Patrol script allows you to move an object along a path made of waypoints. The waypoints are organised in a list (Stops), so you can add, remove, or reorder them easily. The Reset Waypoints button clears the list and then adds only one stop.
The GameObject returns to the starting point once all the waypoints have been walked, and then restart on the path.
As with other Movement scripts, Orientation gives you control on how to orient your Sprite while moving. See its description in Move for move information.
As you create waypoints, they are visualised in the Scene View as Translate handles. You can move them around in here, or in the Inspector by changing the position values.
A little blue arrow marks the direction of movement.
Push applies a continuous force in one direction when holding a specific key on the keyboard. It’s useful to create a controller for vehicles, rockets, etc. and you can use it in combination with Rotate (see below) to allow steering.
In the Scene View, you will see a green arrow gizmo that shows you the direction and strength of the force (see below).
By setting the Axis property, you can control in which direction to push (Y means up, X means to the right). To achieve a push in the opposite direction, simply set Push Strength to a negative value.
In conjunction with Axis, the Relative Axis property controls whether the push "rotates" with the GameObject or whether it is applied in absolute terms. You will not see a change in the gizmo if the object has no rotation. To understand the difference, look at the images below.
With Relative Axis on, rotating the object means that the direction rotates with it (it’s basically in Local Space):
When Relative Axis is off, the direction is absolute (basically in World Space):
The tooltip below the option reflects the change. Most of the time you want to keep it on so that the vehicle moves on its forward direction.
Rotate is a script that applies a torque - that is, a rotation on the Z axis. Like Move, this is controlled with the left/right Arrows, or AD keys. You can use it together with a Push script to create a vehicle-like controller, where you can steer and move forward in the direction the vehicle is pointing to.
|Tip: If you don’t like the centre of rotation of your object, you can parent it to another GameObject and then apply the Rotate script to that one. For example, think of a bicycle, where the centre of rotation is on the back wheel. This way you have much more control on the centre (gizmo), without needing to change it in the actual Sprite asset.|
With Wander, the GameObject randomly moves around in short bursts. The movement speed is controlled by Speed, which as usual goes together with the Friction property in the Rigidbody2D to make it feel right.
Direction Change Interval controls the timing (in seconds) after which the object goes in a new direction. Setting it very low means a lot of shorter, sudden movements.
Keep Near Starting Point means that the GameObject performs a check every now and then, and if it’s straying too far, it heads towards the initial point in its next movement.
|Note: If you set the Speed too high or there’s not enough Friction on the Rigidbody2D, the object might still be able to wander off very far!|
As with other Movement scripts, Orientation gives you control on how to orient your Sprite while moving. See its description in the Move script for move information.