+1 vote

Hi, I am trying to make an AI to play against the pc. The pc is a cannon and has to catch a ball. So I am moving the cannon to the actual ball position, but I need to rotate the cannon to the opposite direction that is facing the ball, like this:

enter image description here

or like this, when ball hits a wall:

enter image description here

I have created an Area2D that detect when the ball enters the area, so I have the ball saved, and I can get the getlinearvelocity() every frame, but is not working, honestly
I don´t know how to do it and I tried a lot of things and I am always getting something that is not the actual direction of the ball

Cannon.rotation = (ball.get_linear_velocity().angle() + deg2rad(180))

that line of code above works for the first image, but not when the ball is hitting something and changing completly the direction like in the second image.

Godot version 3.3
in Engine by (129 points)

1 Answer

0 votes
Best answer

Maybe you should just use look_at method.
Refer to the docs here.

by (1,051 points)
selected by

Yes I tried it too and not worked, the only vector that I tought it was cannon.lookat(ball.getlinear_velocity()), any other vector that I should try?

You should try cannon.look_at(ball.global_position)

Thanks!!, I don´t know why global.position didn´t come to my mind, I feel a little silly, Also I don´t know why but I have to add 90 degrees to the cannon after look_at() to get the cannon working, instead it looked to the left and not to the direction of the ball

The sprite has to face +x direction which is 0°.

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.