Wednesday, March 27, 2019

13. look_at

We have a Sprite with attached script for controlling position and rotation.


The position is controlled with left and right arrows, and rotation is set by the look_at() function. For the look_at function, we can imagine a vector going from sprite center (assuming no offset) to look at point. Here we use a look at point at top of screen. (In project setting we get game width and height both as 400). This is the attached script:



extends Sprite

const TOP = Vector2(200,0)
var old_rot

func _ready():
 position = Vector2(200,200)
 old_rot = int(rotation_degrees)

func _process(delta):
 if Input.is_action_pressed('ui_left'):
  position.x -= 1
 elif Input.is_action_pressed('ui_right'):
  position.x += 1
 look_at(TOP)
 if int(rotation_degrees) != old_rot:
  print("Rotation = ", int(rotation_degrees))
  old_rot = int(rotation_degrees)



The output, when we have moved the icon to left of screen so it keeps pointing to middle-top:


No comments:

Post a Comment