Desired distance during conversation in FPS
Posted: Fri Jul 21, 2023 5:53 am
Issue: If very close to the NPC and start an conversation the player will stay very close to the player and whatever i try to programm the player will never be ''moved away to an desired distance''. That is the issue.
I tried a lot to make the player be pushed backwards a bit when an conversation starts. I did a lot of debugging but nothing works so far. Ideas are welcome!
The script is attached under the ''Usable'' script on the NPC and added to the OnUSe()
I tried a lot to make the player be pushed backwards a bit when an conversation starts. I did a lot of debugging but nothing works so far. Ideas are welcome!
The script is attached under the ''Usable'' script on the NPC and added to the OnUSe()
Code: Select all
private void CheckDistanceAndMovePlayer()
{
float distance = Vector3.Distance(player.transform.position, targetCharacter.transform.position);
Debug.Log("Current distance to NPC: " + distance);
Vector3 directionToPlayer = (player.transform.position - targetCharacter.transform.position).normalized;
Vector3 newPosition = targetCharacter.transform.position + directionToPlayer * requiredDistance;
// The following line will always move your player 10 units away from the NPC, regardless of their initial distance.
player.transform.position = newPosition;
Debug.Log("Moved player to a new position: " + newPosition);
}