游戏 Ursina 的第三人称视角
3rd Person POV for Game Ursina
我正在尝试用 ursina 制作一款角色扮演游戏。我想让相机始终跟随角色的背部。我尝试使用 camera.look_at(player) 但当它旋转时我无法让相机旋转到角色的背面。
app = Ursina()
class character(Entity):
def __init__(self):
super().__init__(
model = load_model('cube'),
color = color.red,
position = (-0, -3, -8)
)
player = character()
print(player.forward)
print(player.forward)
camera.look_at(player)
player.rotation_y =180
def update():
if held_keys['a']:
player.rotation_y -= 2
if held_keys['d']:
player.rotation_y += 2
app.run()```
将相机作为播放器的父对象并将其移回。这样它将与玩家实体一起旋转。
camera.parent = player
camera.z = -10
您可能想要更改 origin
。同样使用 parents
。我稍后会解释这意味着什么。
将origin
(实体移动和旋转的点)更改为它后面的点。
例如
from ursina import * # import urisna
app = Ursina() # make app
player = Entity(model='cube', # this creates an entity of a cube
origin = (0, 0, -2)) #now you have a origin behind the entity
app.run() #run app
但是相机呢,我听到你问了!
我推荐 ursina.prefabs.first_person_controller
它可能专为第一人称控制而设计,但您可以根据自己的目的使用它。
# start by doing the normal thing
from ursina import *
# but also import the first person prefab
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
# create camera and player graphic
cam = FirstPersonController()
player = Entity(model='cube',
origin = (0, 0, -2),
parent = cam)
# run
app.run()
您需要创建一个楼层 entity
。
这是第 3 人称控制器的您所需要的。 parents 和起源确保了这一点。它内置了WASD和箭头键控制,还有鼠标控制。
@Cyber-Yosh 最近针对这个 post 问了一个关于如何在没有第一人称控制器的情况下使用它的问题。就是这样。我已对更改发表评论。
from ursina import * # import as usual
app = Ursina() # create app as usual
window.fps_counter.enabled = False # this is just to remove the fps counter
box = Entity(model='cube', # create cube as before (you can make your own class)
origin=(0,0.7,-5), # set origin to behind the player and above a little
parent=camera, # make it orientate around the camera
color=color.red, # change the color
texture='shore') # choose a nice texture
def update(): # define the auto-called update function
if held_keys['a']:
camera.rotation_y -= 10 * time.dt # the time.dt thing makes it adapt to the fps so its smooth
elif held_keys['d']:
camera.rotation_y += 10 * time.dt
Sky() # just a textured sky to make sure you can see that you are both rotating
app.run() # run
您会注意到我没有创建 class(因为它很容易修改),但我没有使用 load_model
。这是因为即使你使用的是自己的模型,也不需要使用load_model
。只需将文件名(不带文件扩展名)作为 string
。这个好用,我试过了
如果您还有其他问题,请随时提问。我非常乐意提供帮助。如果这有效,请确保 upvote
和 approve
.
我有一个更好的解决方案,因为当使用 FirstPersonController 时,物理会应用到控制器而不是玩家,我的解决方案是创建一个相机:
camera= EditorCamera()
这将创建一个相机,让我们可以看到游戏,接下来我们必须做的是我们必须创建玩家和地板:
terrain_width= 50
player= Entity(model="cube", color= color.orange)
player.x= 0
floor= Entity(model="plane", texture="grass", scale= terrain_width)
完成后,我们将相机贴在播放器上,并调整一些参数以从 above-back:
看到播放器
camera.parent= player
camera.y= 5
camera.z= -10
camera.rotation_x= 9.15
现在我们可以让我们的玩家移动,我们将看到相机也移动:
def input(key):
if key == "a":
player.x-= 1
if key == "d":
player.x+= 1
if key == "w":
player.z+= 1
if key == "s":
player.z-= 1
这将是完整的代码:
from ursina import *
app= Ursina()
camera= EditorCamera()
terrain_width= 50
player= Entity(model="cube", color= color.orange)
player.x= 0
floor= Entity(model="plane", texture="grass", scale= terrain_width)
camera.parent= player
camera.y= 5
camera.z= -10
camera.rotation_x= 9.15
def input(key):
if key == "a":
player.x-= 1
if key == "d":
player.x+= 1
if key == "w":
player.z+= 1
if key == "s":
player.z-= 1
app.run()
希望这对你有用:)
如果有人想使用 FirstPersonController
,我发现了如何将相机从玩家身上移开,同时保持所有物理交互!
创建FirstPersonController
后,修改其camera_pivot
的位置(默认为(0,2,0)
)。
player = FirstPersonController(model="cube", color=color.orange, origin_y=-.5,z=-10)
# the default camera_pivot is (0,2,0)
player.camera_pivot.z = -3.5 # move the camera behind the player model
player.camera_pivot.y = 2.5 # move the camera a little higher
完整示例(q 退出)
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import lit_with_shadows_shader
app = Ursina()
Entity.default_shader = lit_with_shadows_shader
ground = Entity(model='plane', collider='box', scale=64, color=color.green)
player = FirstPersonController(model="cube", color=color.orange, origin_y=-.5,z=-10)
# the default camera_pivot is (0,2,0)
player.camera_pivot.z = -3.5 # move the camera behind the player model
player.camera_pivot.y = 2.5 # move the camera a little higher
# setting collider and making it visible for debugging
player.collider = BoxCollider(player, Vec3(0,1,0), Vec3(1,2,1))
player.collider.visible=True
# adding some objects to collide with
for i in range(16):
Entity(model='cube', origin_y=-.5, scale=2, texture='brick', texture_scale=(1,2),
x=random.uniform(-8,8),
z=random.uniform(-8,8) + 8,
collider='box',
scale_y = random.uniform(2,3),
color=color.hsv(0, 0, random.uniform(.9, 1))
)
sun = DirectionalLight()
sun.look_at(Vec3(1,-1,-1))
Sky()
def input(key):
if key == 'q':
exit()
app.run()
我正在尝试用 ursina 制作一款角色扮演游戏。我想让相机始终跟随角色的背部。我尝试使用 camera.look_at(player) 但当它旋转时我无法让相机旋转到角色的背面。
app = Ursina()
class character(Entity):
def __init__(self):
super().__init__(
model = load_model('cube'),
color = color.red,
position = (-0, -3, -8)
)
player = character()
print(player.forward)
print(player.forward)
camera.look_at(player)
player.rotation_y =180
def update():
if held_keys['a']:
player.rotation_y -= 2
if held_keys['d']:
player.rotation_y += 2
app.run()```
将相机作为播放器的父对象并将其移回。这样它将与玩家实体一起旋转。
camera.parent = player
camera.z = -10
您可能想要更改 origin
。同样使用 parents
。我稍后会解释这意味着什么。
将origin
(实体移动和旋转的点)更改为它后面的点。
例如
from ursina import * # import urisna
app = Ursina() # make app
player = Entity(model='cube', # this creates an entity of a cube
origin = (0, 0, -2)) #now you have a origin behind the entity
app.run() #run app
但是相机呢,我听到你问了!
我推荐 ursina.prefabs.first_person_controller
它可能专为第一人称控制而设计,但您可以根据自己的目的使用它。
# start by doing the normal thing
from ursina import *
# but also import the first person prefab
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
# create camera and player graphic
cam = FirstPersonController()
player = Entity(model='cube',
origin = (0, 0, -2),
parent = cam)
# run
app.run()
您需要创建一个楼层 entity
。
这是第 3 人称控制器的您所需要的。 parents 和起源确保了这一点。它内置了WASD和箭头键控制,还有鼠标控制。
@Cyber-Yosh 最近针对这个 post 问了一个关于如何在没有第一人称控制器的情况下使用它的问题。就是这样。我已对更改发表评论。
from ursina import * # import as usual
app = Ursina() # create app as usual
window.fps_counter.enabled = False # this is just to remove the fps counter
box = Entity(model='cube', # create cube as before (you can make your own class)
origin=(0,0.7,-5), # set origin to behind the player and above a little
parent=camera, # make it orientate around the camera
color=color.red, # change the color
texture='shore') # choose a nice texture
def update(): # define the auto-called update function
if held_keys['a']:
camera.rotation_y -= 10 * time.dt # the time.dt thing makes it adapt to the fps so its smooth
elif held_keys['d']:
camera.rotation_y += 10 * time.dt
Sky() # just a textured sky to make sure you can see that you are both rotating
app.run() # run
您会注意到我没有创建 class(因为它很容易修改),但我没有使用 load_model
。这是因为即使你使用的是自己的模型,也不需要使用load_model
。只需将文件名(不带文件扩展名)作为 string
。这个好用,我试过了
如果您还有其他问题,请随时提问。我非常乐意提供帮助。如果这有效,请确保 upvote
和 approve
.
我有一个更好的解决方案,因为当使用 FirstPersonController 时,物理会应用到控制器而不是玩家,我的解决方案是创建一个相机:
camera= EditorCamera()
这将创建一个相机,让我们可以看到游戏,接下来我们必须做的是我们必须创建玩家和地板:
terrain_width= 50
player= Entity(model="cube", color= color.orange)
player.x= 0
floor= Entity(model="plane", texture="grass", scale= terrain_width)
完成后,我们将相机贴在播放器上,并调整一些参数以从 above-back:
看到播放器camera.parent= player
camera.y= 5
camera.z= -10
camera.rotation_x= 9.15
现在我们可以让我们的玩家移动,我们将看到相机也移动:
def input(key):
if key == "a":
player.x-= 1
if key == "d":
player.x+= 1
if key == "w":
player.z+= 1
if key == "s":
player.z-= 1
这将是完整的代码:
from ursina import *
app= Ursina()
camera= EditorCamera()
terrain_width= 50
player= Entity(model="cube", color= color.orange)
player.x= 0
floor= Entity(model="plane", texture="grass", scale= terrain_width)
camera.parent= player
camera.y= 5
camera.z= -10
camera.rotation_x= 9.15
def input(key):
if key == "a":
player.x-= 1
if key == "d":
player.x+= 1
if key == "w":
player.z+= 1
if key == "s":
player.z-= 1
app.run()
希望这对你有用:)
如果有人想使用 FirstPersonController
,我发现了如何将相机从玩家身上移开,同时保持所有物理交互!
创建FirstPersonController
后,修改其camera_pivot
的位置(默认为(0,2,0)
)。
player = FirstPersonController(model="cube", color=color.orange, origin_y=-.5,z=-10)
# the default camera_pivot is (0,2,0)
player.camera_pivot.z = -3.5 # move the camera behind the player model
player.camera_pivot.y = 2.5 # move the camera a little higher
完整示例(q 退出)
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import lit_with_shadows_shader
app = Ursina()
Entity.default_shader = lit_with_shadows_shader
ground = Entity(model='plane', collider='box', scale=64, color=color.green)
player = FirstPersonController(model="cube", color=color.orange, origin_y=-.5,z=-10)
# the default camera_pivot is (0,2,0)
player.camera_pivot.z = -3.5 # move the camera behind the player model
player.camera_pivot.y = 2.5 # move the camera a little higher
# setting collider and making it visible for debugging
player.collider = BoxCollider(player, Vec3(0,1,0), Vec3(1,2,1))
player.collider.visible=True
# adding some objects to collide with
for i in range(16):
Entity(model='cube', origin_y=-.5, scale=2, texture='brick', texture_scale=(1,2),
x=random.uniform(-8,8),
z=random.uniform(-8,8) + 8,
collider='box',
scale_y = random.uniform(2,3),
color=color.hsv(0, 0, random.uniform(.9, 1))
)
sun = DirectionalLight()
sun.look_at(Vec3(1,-1,-1))
Sky()
def input(key):
if key == 'q':
exit()
app.run()