Scripting

Search The Documentation:

  1. Home
  2. Docs
  3. Scripting
  4. Examples
  5. Splitscreen

Splitscreen

Platform Compatibility

PC Mac OSX Oculus Quest
X X

Simple two player controls, set player number, make sure there is an object in the scene with tag Alpha for player one, and Beta for player two, then two camera objects with this script on them (and either player = 1 for player one, or player = 2 for player 2).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
--This script belongs on a camera object
--It will only work if it finds the object it is looking for
player = 1
moveSpd = 10
rotSpd = 60

--How to position the camera behind our object
camoffset = "0, 1, -1"

--Player one controls
fwdkey = 'w'
backkey = 's'
leftkey = 'a'
rightkey = 'd'

findtag = 'Alpha'

if player == 1 then
    --This could easily be expanded to more players just by adding more checks for player number
    --Eg: top-left would be 0,0,0.5,0.5, top-right 0.5,0,0.5,0.5, bottom-left 0,0.5,0.5,0.5, bottom-right 0.5,0.5,0.5,0.5
    obj.setCamViewPort(0, 0, 0.5, 1)
else
    obj.setCamViewPort(0.5, 0, 0.5, 1)
   
    --Player two controls
    fwdkey = 'i'
    backkey = 'k'
    leftkey = 'j'
    rightkey = 'l'
   
    findtag = 'Beta'
end

myplayer = obj.tryStore(findtag)

--Check if we have a player, then make ourselves attached to them
if obj.notNull(myplayer) then
    obj.setParent(-1, myplayer)
    obj.moveStored(camoffset, -1, true)
   
    --Found it! enable our camera and disable the player
    obj.setCamEnabled(true)
    obj.setPlayerEnabled(false)
    obj.setPlayerHidden(true)
end

function onFrameTick(dt)
    if obj.notNull(myplayer) then

        --Get player input
        local xIn = 0
        local yIn = 0
   
        if obj.checkKey(fwdkey) then
            yIn = moveSpd * dt
        end
   
        if obj.checkKey(backkey) then
            yIn = -1.0 * moveSpd * dt
        end
   
        if obj.checkKey(leftkey) then
            xIn = -1.0 * rotSpd * dt
        end
   
        if obj.checkKey(rightkey) then
            xIn = rotSpd * dt
        end
   
        --Move and rotate
        --Get the forward direction of the player object
        local fwd = obj.getForward(myplayer)
       
        --Break it into X,Y,Z values array, LUA starts at 1 not 0!
        local components = obj.split(fwd, ',')
       
        --Multiply each value by yIn
        local newmove = components[1] * yIn .. ',' .. components[2] * yIn .. ',' .. components[3] * yIn
        --This could also be done with obj.scaleVector3(components[1], components[2], components[3], yIn)
       
        --Move by this offset
        obj.translateStored(newmove, myplayer)
       
        --Rotation is simple, we just rotate on the Y axis (Yaw)
        local rot = "0," .. xIn .. ",0"
        obj.rotateStored(rot, myplayer)
    end
end
Tags
Was this article helpful to you? Yes No

How can we help?