Working on 2D mode, I have a script attached to a Sprite. The Update()
part is as follow:
void Update () {
if (Input.GetKeyDown ("left")) {
cursor.rigidbody2D.MovePosition (cursor.rigidbody2D.position - speedX);
} else if (Input.GetKeyDown ("right")) {
cursor.rigidbody2D.MovePosition (cursor.rigidbody2D.position + speedX);
} else if (Input.GetKeyDown ("up")) {
cursor.rigidbody2D.MovePosition (cursor.rigidbody2D.position + speedY);
} else if (Input.GetKeyDown ("down")) {
cursor.rigidbody2D.MovePosition (cursor.rigidbody2D.position - speedY);
}
}
where cursor
is a Prefab linked in Inspector. The cursor
prefab has only 2 components, Sprite Renderer (to define the image), and the Rigidbody 2D, which setting is as follow:
- Mass = 1
- Linear Drag = 0
- Angular Drag = 0
- Gravity Scale = 0
- Fixed Angle = true
- Is Kinematic = false
- Interpolate = None
- Sleeping Mode = Start Awake
- Collision Detection = Discrete
But when I press the arrow keys, the sprite showing on screen does not move. What did I miss?
Tried to add Debug.Log()
inside the if
case, it really enters the case. And no error occurred.
Note: speedX = new Vector2(1,0);
and speedY = new Vector2(0,1);
cursor
really linked with the prefab or with an instance of it? If it is linked with an object in your scene than the script should work. – Stefan HoffmannOnMouseDown()
event, whichInstantiate()
the Prefab on screen. – Raptor