Distance to the animal
Discussion in 'Rust Development' started by maksiyshuk, Jun 1, 2015.
-
You can use: http://docs.unity3d.com/ScriptReference/Vector3.Distance.html I don't know if there is an example out there though.
It should be something like this:
local playerPos = player.transform.position
local animalPos = entity.transform.position --You'll need to get the entity first.
local distance = UnityEngine.Vector3.Distance( playerPos, animalPos)
print("Distance to animal: " .. tostring(distance)) -
I want the distance to the animal from which it was killed - could be defined as a variable and continue to use it as an example of the award in the amount of money equal to the distance!
-
Yeah and what is stopping you from doing that? I gave you all the code you need to find the distance, you will need to figure out the rest.
-
Take off the 'Distance'.Code:
local distance = UnityEngine.Vector3( playerPos, animalPos)
-
UnityEngine.Vector3 - don't work, but UnityEngine.Vector3.Distanc - work

[DOUBLEPOST=1433183576][/DOUBLEPOST]How to round the value of the distance to the nearest whole number or two numbers after the decimal point?Last edited by a moderator: Jun 1, 2015 -
Oh nvm mind you're right:
Code:def distance(self, p1, p2): return Vector3.Distance(p1, p2) -
How to round the value of the distance to the nearest whole number or two numbers after the decimal point?
-
Wulf Community Admin
-
I write, but nothing happens!
function round(distance, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", distance))
end
[DOUBLEPOST=1433196984][/DOUBLEPOST]Here shows the distance to all the animals, but not for as I kill! -
I haven't test it, but should work.[/code]Code:
local playerPos = player.transform.position local animalPos = entity.transform.position local distance = UnityEngine.Vector3.Distance( playerPos, animalPos) local rounded = math.floor(string.format('%.2f', num))print('Distance: ' .. rounded)Last edited by a moderator: Jun 2, 2015 -
also should workCode:
local xr = string.format("%.0f", animal.transform.position.x) local zr = string.format("%.0f", animal.transform.position.z) local xk = string.format("%.0f", player.transform.position.x) local zk = string.format("%.0f", player.transform.position.z) local dist = math.floor(math.sqrt(math.pow(xr - xk,2) + math.pow(zr - zk,2))) print(dist)
