movement update
This commit is contained in:
@@ -28,7 +28,8 @@ public class GeminiResponse
|
||||
{
|
||||
public Candidate[] candidates;
|
||||
}
|
||||
public class GerminiNPC :MonoBehaviour
|
||||
|
||||
public class GerminiNPC : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private string apiKey = "AQ.Ab8RN6I2hU_p8yHiPNNHtWzYBiLugbPP22gC6lzTWaYEWj4v0g";
|
||||
@@ -37,46 +38,96 @@ public class GerminiNPC :MonoBehaviour
|
||||
"https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent";
|
||||
|
||||
public string npcPersona =
|
||||
$"Nguơi là ột thợ lão thợ rèn cọc cằn tên là TOm,ngươi rất ghét những kẻ mang phế liệu đến tiệm của mình.Ch trả lời ngắn gọn trong 2 câu,theo phong cách trung cổ";
|
||||
"Ngươi là một lão thợ rèn cọc cằn tên là Tom, ngươi rất ghét những kẻ mang phế liệu đến tiệm của mình. Chỉ trả lời ngắn gọn trong 2 câu, theo phong cách trung cổ.";
|
||||
|
||||
public string playerHeldItem = "Thanh kiếm rỉ sét";
|
||||
public float interactionDistance = 5f; // Khoảng cách tối đa để nói chuyện
|
||||
public Transform playerTransform; // Gán transform của Player vào đây
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Keyboard.current.spaceKey.wasPressedThisFrame)
|
||||
if (Keyboard.current != null && Keyboard.current.fKey.wasPressedThisFrame)
|
||||
{
|
||||
StartCoroutine(GetGerminiReponse());
|
||||
if (CanSeePlayer())
|
||||
{
|
||||
StartCoroutine(GetGerminiReponse());
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("<color=yellow>Hệ thống:</color> Bạn ở quá xa hoặc bị tường che khuất!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanSeePlayer()
|
||||
{
|
||||
if (playerTransform == null)
|
||||
{
|
||||
// Tự tìm player nếu chưa gán
|
||||
GameObject player = GameObject.FindGameObjectWithTag("Player");
|
||||
if (player != null) playerTransform = player.transform;
|
||||
else return false;
|
||||
}
|
||||
|
||||
// 1. Check khoảng cách
|
||||
float dist = Vector3.Distance(transform.position, playerTransform.position);
|
||||
if (dist > interactionDistance) return false;
|
||||
|
||||
// 2. Check xem có bị tường che không (Raycast)
|
||||
Vector3 direction = (playerTransform.position + Vector3.up) - (transform.position + Vector3.up);
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(transform.position + Vector3.up, direction, out hit, interactionDistance))
|
||||
{
|
||||
if (hit.collider.CompareTag("Player") || hit.collider.transform.IsChildOf(playerTransform))
|
||||
{
|
||||
return true; // Thấy đầu/người player
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private IEnumerator GetGerminiReponse()
|
||||
{
|
||||
var jsonBody = $@"{{""systemInstruction"": {{""parts"": [{{ ""text"": ""{npcPersona}"" }}]}},
|
||||
""contents"": [{{""parts"": [{{ ""text"": ""Ta muốn bán cho ông món đồ này cho ông {playerHeldItem}""}}]}}] }} ";
|
||||
var requestURL = $"{germiniURL}?ket={apiKey}";
|
||||
using (var request = new UnityWebRequest(germiniURL, "POST"))
|
||||
var jsonBody = $@"{{
|
||||
""systemInstruction"": {{""parts"": [{{ ""text"": ""{npcPersona}"" }}]}},
|
||||
""contents"": [{{""parts"": [{{ ""text"": ""Ta muốn bán cho ông món đồ này: {playerHeldItem}""}}]}}]
|
||||
}}";
|
||||
|
||||
// 1. Sửa tham số thành ?key= (trước đó là ?ket=)
|
||||
var requestURL = $"{germiniURL}?key={apiKey}";
|
||||
|
||||
// 2. Sử dụng requestURL (có chứa key) thay vì germiniURL gốc
|
||||
using (var request = new UnityWebRequest(requestURL, "POST"))
|
||||
{
|
||||
var bodyRaw = Encoding.UTF8.GetBytes(jsonBody);
|
||||
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
|
||||
yield return request.SendWebRequest();
|
||||
if (request.result == UnityWebRequest.Result.ProtocolError)
|
||||
|
||||
if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
|
||||
{
|
||||
Debug.LogError(request.error);
|
||||
Debug.LogError($"[Gemini Error] {request.error} - Response: {request.downloadHandler.text}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var responseTEXT = request.downloadHandler.text;
|
||||
var germiniResponse=JsonUtility.FromJson<GeminiResponse>(responseTEXT);
|
||||
if (germiniResponse.candidates.Length > 0)
|
||||
try
|
||||
{
|
||||
var npcResponse = germiniResponse.candidates[0].content.parts[0].text;
|
||||
Debug.Log(npcResponse);
|
||||
var geminiResponse = JsonUtility.FromJson<GeminiResponse>(responseTEXT);
|
||||
if (geminiResponse != null && geminiResponse.candidates != null && geminiResponse.candidates.Length > 0)
|
||||
{
|
||||
var npcResponse = geminiResponse.candidates[0].content.parts[0].text;
|
||||
Debug.Log($"<color=green>Tom:</color> {npcResponse}");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[JSON Parse Error] {e.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user