how to make 3d character act 2d

How to make a 3d character point left instantly and right instantly?This is for a 2d game i am making.

here is an example
upravlenie primer.blend (1.01 MB)

I am sorry but i would like a python solution to the problem.


from bge import logic

cont = logic.getCurrentController()
own = cont.owner

own.worldPosition.x = 0.0 #replace the axis and number as you need

Always(true level triggering) --> Python(TheSrcipAbove.py)

Thankyou for the script.I will try it.

This will assume the character is locked to the world X axis, positive world Y is right and negative world Y is left. The character’s forward facing is their positive Y axis.
For this, we are using alignAxisToVect()


own = bge.logic.getCurrentController().owner
def faceLeft():
    own.alignAxisToVect( [0,-1,0], 1, 1)

def faceRight():
    own.alignAxisToVect( [0,1,0], 1, 1)

own.worldLinearVelocity.x*=0
helps as well.

This python code did not work.Does the python controller have to be in module mode.

from bge import logic
own = bge.logic.getCurrentController().owner
def faceLeft():
    own.alignAxisToVect( [0,-1,0], 1, 1)
def faceRight():
    own.alignAxisToVect( [0,1,0], 1, 1)

Yes you need module mode, if you see “def” this means this is a module. You have two modules in this script: “faceLeft()” and “faceRight()”.
The script name must have .py in it!
To activate put the scriptname.faceLeft into the module

You can then connect the appropriate sensors to activate it
(e.g. add a keyboard sensor (with tap) “left arrow” to the module “scriptname.faceLeft” and then this will only activate when you press it)

This is the error i get.
http://www.pasteall.org/blend/36777

Blender Game Engine Started
Python module can't be imported - object 'Cube.001', controller 'And':
Traceback (most recent call last):
  File "C:\Users\barbara\Desktop\my scroller.blend\facing.py", line 3, in <modul
e>
NameError: name 'bge' is not defined
Blender Game Engine Finished

Use


import bge

on top of code:D
or use


own = logic.getCurrentController().owner

Your choice!

import bge
own = bge.logic.getCurrentController().owner
def faceLeft():
    own.alignAxisToVect( [0,-1,0], 1, 1)
def faceRight():
    own.alignAxisToVect( [0,1,0], 1, 1)

That stood him on his head.

import bge
own = bge.logic.getCurrentController().owner
def faceLeft():
    own.alignAxisToVect( [-1,0,0], 1, 1)
def faceRight():
    own.alignAxisToVect( [1,0,0], 1, 1)

That stood him on his head facing forwards.

import bge
own = bge.logic.getCurrentController().owner
def faceLeft():
    own.alignAxisToVect( [0,0,-1], 1, 1)
def faceRight():
    own.alignAxisToVect( [0,0,1], 1, 1)
That laid him on his side.

Which is it?

What is the forward facing axis of your character? Which world axis is the camera pointing toward?

Above the code I posted, I placed assumptions about how these would be set up. You probably have them set up differently, and need to modify the vectors to reflect that.

As for the character standing on his head, you could remedy that with a third alignAxisToVect on his Z axis (assuming again that positive Z is ‘up’):

A little more robustness to the code…


from bge import logic, events


def faceLeft(own):
    own.alignAxisToVect([0,-1,0],1,1)
    
def faceRight(own):
    own.alignAxisToVect([0,1,0],1,1)




def main():
    
    own = logic.getCurrentController().owner
    keys = logic.keyboard
    
    own.alignAxisToVect([0,0,1],2,1)
    
    LEFT = logic.KX_INPUT_JUST_ACTIVATED == keys.events[events.LEFTARROWKEY]
    RIGHT = logic.KX_INPUT_JUST_ACTIVATED == keys.events[events.RIGHTARROWKEY]
    
    if LEFT:    faceLeft(own)
    if RIGHT:    faceRight(own)

Place that in a python controller in Module mode and have it call ‘thisFile.main’ (thisFile.py being the file you’re going to have this code in). Noodle it to an Always (pos pulse ON) sensor and it should be good to go, although it’s entirely untested.

The character should turn left and right with the left and right arrow keys. The alignAxisToVect in the main loop should keep him from flipping onto his head.

I am using the state actuator to switch between these two scripts.The first script makes my character stand on his head.How would i make my character face forwards with that script?

import bge
own = bge.logic.getCurrentController().owner
def faceLeft():
    own.alignAxisToVect( [0,1,0], 1, 1)
def faceRight():
    own.alignAxisToVect( [0,-1,0], 1, 1)
def faceStraight():    
    own.alignAxisToVect([0,0,1],2,1)

This script makes my character stand on his head.

import bge
own = bge.logic.getCurrentController().owner
def faceLeft():
    own.alignAxisToVect( [0,-1,0], 1, 1)
def faceRight():
    own.alignAxisToVect( [0,1,0], 1, 1)
def faceStraight():    
    own.alignAxisToVect([0,0,1],2,1)

This script makes my character stand on his feet facing backwards.

Why do you need a script (especially as you seems not to be confident with Python)?

Simply turn your character in 180° steps.

To make a side-scroller videogame.You know what those types of videogames are?

Yes, I know this type of games.

You are right I should be a bit more precise … turn 180° around Z-Axis. A motion actuator can do that … even an action. Yes, a python controller can do this as well.

I am going to use the invisibilty actuator for that.It seems like a great solution to me.