0 votes

I am developing a 3d turn based strategy game. Right now I have pawns that I can move around the grid and a camera I control with the arrow keys. I want the camera to also be able to follow the pawns when they are selected and when they move. The camera is the only child of a spatial node and the script is attached to the spatial node.

So far, I have been able to have the camera always centered on the pawns with the following code run through the physicsprocess():

func _track_pawn(selected_pawn):
  var from = self.get_translation()
  var to = selected_pawn.get_translation() + Vector3(0, -3, 0)
  if from != to:
    var move = from.linear_interpolate(to, .2)
    self.set_translation(move)

The problem, of course, is that this prevents the camera from being moved with the arrow keys because it's always following the selected pawn around. On the other hand, I can run the same code through an input function and the camera will scoot a little way towards the pawn whenever the input is pressed.

I can, of course, just use:

    func _track_pawn(selected_pawn):
      var to = selected_pawn.get_translation() + Vector3(0, -3, 0)
      self.set_translation(to)

and that will jump immediately to whichever pawn is selected when it's selected, but it's a bit jarring. I have also tried using a while loop, but the result is a crash:

func _track_pawn(var selected_pawn):
  var from = self.get_translation()
  var to = selected_pawn.get_translation() + Vector3(0, -3, 0)
  while from != to:
            var move = from.linear_interpolate(to, .2)
            self.set_translation(move)

Any ideas?

Godot version 3.3
in Engine by (15 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.