My player companion is having a problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RRocky123

I have a player (as a dog) and i want to give him a companion (or friend)for his mission and other stuff. I want this companion to follow this player.I made the companion follow the player.

 get_node("Companion").dir = (get_node("player dog").position - get_node("Companion").position).normalized()

this is the var direction of my player’s companion
In my companion code i have the below code:
#########################
extends Node2D

var dir = Vector2()
var speed = 500

func _physics_process(delta):
var velocity = dirspeeddelta
translate(velocity)
##################

My companion is a node2D, now the problem the companion keeps on shaking when he reaches near the player(dog).
It moves back and forth fastly
plz help and ask if you want more details…

:bust_in_silhouette: Reply From: Bernard Cloutier

Check the distance between Companion and player dog. If distance is less than 1 (or whatever disrance you want the companion to stay at), don’t change it’s position.

The reason he’s going back and forth is because he’s at like 0.05 distance from the player to his right, so he goes “I have to go left!” and moves a bit over 0.05 distance. But now next frame he’s at like 0.02 units to the left and he goes “I have to go right!” and so on.

it worked thanks alot…

RRocky123 | 2020-10-19 15:39

it follows the player keeping the distance i want but now i have this other problem,the companion is following but when ever i move left or right it starts to buffer(back and fourth) keeping the distance between player and companion.Now what to do plz help…

RRocky123 | 2020-10-19 16:00