Jump to content

[Help] How to move a collection of entities / objects


Papa Beans
 Share

Recommended Posts

Hey guys, me again. I was curious how one would go about moving a bunch of objects all at once. Basically I decided to go with a car game as my first one. Just to get the hang of things. Now, in my editor you see this [its an image] http://gyazo.com/5094ed8b0529ac914a2675f5a3e879a1 [<<Click]. Then I go into the scripting area and the objects wont turn with

--Script is bound to the 'truck'
self.entity:Move(speed, 0,0)

I have tried entity turn, and others but they dont wanna turn. How can I make them all turn at once (act like one object)? Thank you for your time,

 

Cody Mihelich

Link to comment
Share on other sites

Can we see the entire script? What is speed? If it's a parameter to the script you should have self.speed not just speed. You are also passing speed to the x axis, where I would think you'd want to pass it to the z axis (forward/backward). Also note that Move() doesn't use any physics. If you would want to use physics you'd probably want AddForce(...).

Link to comment
Share on other sites

Can we see the entire script? What is speed? If it's a parameter to the script you should have self.speed not just speed. You are also passing speed to the x axis, where I would think you'd want to pass it to the z axis (forward/backward). Also note that Move() doesn't use any physics. If you would want to use physics you'd probably want AddForce(...).

 

 

I have self.speed, it is a parameter in the script. Just wanted to show the basic idea. And I also realized that and changed to the Z axis, no cigar. And should I need physics for this? I did call it a character controller, and used the default shape.phy for the prefab. And I started this tonight so it is pretty hardcoded, but I put this in App.lua under the loop function

if self.window:KeyHit(Key.W) then
 if truck.forward == true then
  truck.forward = false
 else
  truck.forward = true
 end
end
if self.window:KeyHit(Key.S) then
 if truck.back == true then
  truck.back = false
 else
  truck.back = true
 end
end
if self.window:KeyHit(Key.A) then
 if truck.left == true then
  truck.left = false
 else
  truck.left = true
 end
end
if self.window:KeyHit(Key.D) then
 if truck.right == true then
  truck.right = false
 else
  truck.right = true
 end
end

And then in the script attached to the truck I have

Script.moveSpeed = 7.5 --int

function Script:Start()
forward = false
left = false
right = false
back = false
end
function Script:UpdateWorld()
  end

function Script:UpdatePhysics()

end
--[[
function Script:Collision(entity, position, normal, speed)

end
]]--
--[[
function Script:Draw()

end
]]--
--[[
function Script:DrawEach(camera)

end
]]--
function Script:PostRender(context)
if self.forward == true then
		   -- Just moved this section down. Doesnt look like it even knows that the forward was toggled, yet my UI script does?
 --self.entity:AddForce(0,0,10)
 App.context:DrawText("Coming from truck.", 200, 220)
end
end
--[[
function Script:Release()

end
]]--
--[[
function Script:Cleanup()

end
]]--

Link to comment
Share on other sites

problems :

1) If you use a character controller as physics for your truck , you must use SetInput function and not other functions for

simple primtives physics (addForce ,AddTorque, PhysicSetPosition,PhysicsSetRotation)

1 ) You put some entity movement on PostRender function., this is the wrong place, you must put your moving truck code

on PhysicsUpdate function

2) the entity.AddForce line that could make move some box physics shape is a commented line

3) You must pass variables from one script to another like : forward, backward etc ...

So declare general variables or get variables values from other script by accessing them using script name.

 

Once you'll get a moving cube working than you will be able to replace it by your truck and child entities.

Stop toying and make games

Link to comment
Share on other sites

How does App.lua know what 'truck' is? App.lua doesn't automatically get variables by the name of what they are in the scene. So what you are doing in App.lua with truck variable won't work the way you think it will. All that stuff should be in your truck script. Remember you can access the window created in App.lua inside any script via App.window. So in your truck script you should have all those checks for buttons being pressed and instead of truck variable do self.entity (which because it's attached to the truck model will mean the truck in your scene).

 

Remember that 95% of the time you won't even touch App.lua (at least when starting out). Use the entity scripts as self contained little programs that control entities they are attached to.

Link to comment
Share on other sites

@Papa Beans :

just make a floor cube, and a small truck BSP cube with a mass.

Create a new Lua script with code below and attach it ot the small truck cube, you should be able with UP/DOWN keys to move the small cube.

 

 

 



Script.window = Window:GetCurrent()

Script.move  = Vec3()

function Script:Start()

self.rotation = self.entity:GetRotation()

self.entity:SetFriction(0,0) 
self.move = Vec3(0,0,0)
end




function Script:UpdatePhysics()

self.move = Vec3(0,0,0)

       local movespeed= 10 
       if (self.window:KeyDown(Key.Up)) then self.move.x= movespeed * Time:GetSpeed() end
       if (self.window:KeyDown(Key.Down)) then self.move.x= -1 *  movespeed * Time:GetSpeed() end      

       self.entity:AddForce(self.move.x,0,self.move.z) 

       Time:Update()
end

Stop toying and make games

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...