Controlling player's view (camera)

Post Python examples to help other users.
User avatar
iPlayer
Developer
Posts: 498
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Controlling player's view (camera)

Postby iPlayer » Wed Apr 19, 2017 3:06 pm

This snippet allows you to detach player's camera from him and make the player see the map from your desired location.
You can also tie player's camera to another entity.

Syntax: Select all

from entities.entity import Entity
from entities.helpers import baseentity_from_index
from events import Event
from players.dictionary import PlayerDictionary


cameras = PlayerDictionary(factory=lambda index: None)


def enable_camera(index, origin, angles):
if cameras[index] is not None:
disable_camera(index)

camera = cameras[index] = Entity.create('point_viewcontrol')
camera.spawn_flags = 8 # "Infinite Hold Time"
camera.spawn()

# Set the angles
camera.angles = angles

# Set the origin (TODO: Might be possible to set angles here, too?)
camera.teleport(origin, None, None)

# You may also tie your camera to an entity pointer!
# camera.set_parent(other_entity.pointer, -1)

base_entity = baseentity_from_index(index)
camera.call_input('Enable', activator=base_entity, caller=base_entity)


def disable_camera(index):
camera = cameras.pop(index, None)
if camera is None:
return

camera.call_input('Disable')
camera.remove()


@Event('round_start')
def on_round_start(game_event):
for camera in cameras.values():
if camera is None:
continue

camera.call_input('Disable')
camera.remove()

cameras.clear()


Usage:

Syntax: Select all

from commands.typed import TypedSayCommand
from mathlib import Vector


@TypedSayCommand(['camera', 'enable'])
def typed_camera_enable(
command_info, x:int, y:int, z:int, pitch:int=0, yaw:int=0, roll:int=0):

origin = Vector(x, y, z)
angles = Vector(pitch, yaw, roll)

enable_camera(command_info.index, origin, angles)


@TypedSayCommand(['camera', 'disable'])
def typed_camera_disable(command_info):
disable_camera(command_info.index)


On CS:GO, de_dust2 the following chat command will let you see the long:

Code: Select all

camera enable 1400 2100 70 0 270 0
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots

Hail, Companion. [...] Hands to yourself, sneak thief. Image

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 1 guest