I want to rotate the spear to the chicken when it is moving. I use the code from the rotate example but does not work. I do not have the camera game object. How can I make it work?
local to = go.get_position()
local from = go.get_position("chicken")
local angle = math.atan2(to.x - from.x, from.y - to.y)
rotation = vmath.quat_rotation_z(angle)
print(angle)
go.set_rotation(rotation)
I update my code and it works well from 0 to 90 deg. But if the chicken move out of 90 deg, the spear stop rotating.
local from = go.get_position("chicken")
local direction = from - to
local angle = math.atan2(direction.y, direction.x)
local rotation = vmath.quat_rotation_z(angle)
go.set_rotation(rotation)
end
local from = go.get_position(hash("/arrow"))
local to = go.get_position(hash("/target"))
local direction = to-from
local angle = math.atan2(direction.y, direction.x)
local rotation = vmath.quat_rotation_z(angle)
go.set_rotation(rotation, hash("/arrow"))
@britzl I use @COCO code and it works. But I have to put the spear to a new game object that is sibling of hunter in the collection. The code I used before did not work because the spear was the child of the hunter. How do I change the code for the child object?