Page 1 of 1

Desired distance during conversation in FPS

Posted: Fri Jul 21, 2023 5:53 am
by Konst
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. :idea: 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);
    }

Re: Desired distance during conversation in FPS

Posted: Fri Jul 21, 2023 7:49 am
by Konst
I found an solution. I added an second camera and when an conversation starts there is an empty gameobject on the NPC, the camera flies towards the location of that gameobject and will LookAt the NPC.

Moving my player or main camera is not possibel i think because of how the controls are setup. But this works.

Re: Desired distance during conversation in FPS

Posted: Fri Jul 21, 2023 8:46 am
by Tony Li
Hi,

If you do want to move your player during conversations, maybe your FPS controller scripts keeping the player from moving. If so, maybe you could disable those scripts when the conversation starts and then re-enable them when the conversation ends. Immediately after disabling the scripts, try moving the player. The Interaction Tutorial video (at 07:00) explains how to do things such as disable scripts when a conversation starts.