Any way to make the rotation of the char based on the horizontal movement of the mouse? I know there’s a way to detect mouse movement but not to see how much it moved and if it moved horizontal :S
With mouse movement, you have to use a Python script to track it. The easiest way to do what you want is to set up a rotation animation for the character (probably 360 degrees around the Z-axis), set the animation’s IPO extend mode to “Extrapolation” (this option is in the IPO window, in the Curve menu under “Extend Mode”), then give the character these logic bricks:
Mouse (Movement) >> Python (mouseMove.py) >> IPO (Property: frame)
Click the “Add Property” button in the logic panel and name the new property “frame”. Leave it as a float type variable, and set its initial value to 1.000. Add a second property named “oldX”, also a float.
Now bring up a Text Editor window in Blender and make a new text file. Name it mouseMove.py (you can’t enter the script name in the Python controller until this is done). Type this code:
cont = GameLogic.getCurrentController()
own = cont.getOwner()
move = cont.getSensor(“type your mouse move sensor name here”)
anim = cont.getActuator(“type your IPO actuator name here”)movement = move.getXPosition()
scale = 1change the scale factor to speed up or slow down the rotation
own.frame -= (own.oldX - movement)*scale
own.oldX = movement
GameLogic.addActiveActuator(anim,1)
That should work. I based it off of the (working) rotate script I wrote yesterday for a game my team is currently working on.
Edit: Oops. That was supposed to be a -= in the script, not a +=. Otherwise the rotation is backward. Fixed it.
Re-edit: Fixed code. Should work now.
thanks but it ain’t working, no error, nothing :S
Sorry. I see that I forgot one very important line of code. At the end of the script, add this line:
GameLogic.addActiveActuator(move,1)
I’ll update my original post to fix that.
Edit: This is wrong. The word “move” should be “anim”. Fixed in original post.
It doesn’t seem to work for me as well…Abel did you add the actuator nameand sensor name in the python?
hmmm…I tryed and it doesn’t make a difference but on the post above this one theres a space between Actuator and (move,1)…Your originall doesn’t have it…I’m going to continue to try to figure this out though
ok here’s my .blend i think I’ve done it right but it doesn’t seem to work it’s named blendenzosmouse.blend i think here
Argh! I was in a hurry typing the fix (because I’m at work) and I screwed it up again. The code should be:
GameLogic.addActiveActuator(anim,1)
You need to activate the animation actuator, and what I typed earlier was the movement sensors. I’m changing the original post again to reflect that.
It really, really, really should work this time.
BTW, there should be no space between the “addActiveActuator” and the “(anim, 1)”. It only looks like there is one there because I used the code tags. I accidentally used quote tags at the top, that’s why it looks different.
lol thanks blendenzo it’s apreciated:D
Trying again
edit:…IT WORKS!!! WOOOO!!!..=D
edit: Cool so is there a simple way to limit the up and down movement i made by editing your code?..btw it’s updated on the site up there
tested ur file it worked, tried to make it work with mine it doesn’t :S so u might look at my file see what i’ve done wrong :S Also for some reason I can’t even make it do an action when u press a button so any help would be nice
it’d probably be some noobstuff, i just started with tring out BGE
http://s29.quicksharing.com/v/1131461/gamechar.blend.html
k 1 sec abel:D ill help…slow download…almost done download now…uhoh I’m not sure what’s wrong…you may want to download my file append the cube in and go into edit mode delete the cube and parent the cube to ur objects…that’s all i can do sorry
Abel, I’m at work right now and I don’t have Blender on this PC. I’ll look at your file when I get home (in about seven hours).
Chirps, to limit the up and down, just don’t make an IPO that goes all the way up and down. I’ll explain better later.
Ok did that and now if i set the extend thing to cyclic it kinda works but once it reaches the top or bottom it twitches back to the first place…
Cool. You’ve almost got it. You just need to limit the mouselook to the first and last frames of the animation. I’m on lunch break, so I’ve got some time to give the whole step by step for anyone who’s interested.
The rotation animation for the up/down movement should be on a separate object from the side to side rotation. Here’s how I usually set it up:
- The Player is the parent of Empty. Player has the horizontal rotation IPO, and Empty has the vertical rotation IPO.
- Empty is the parent of the Camera.Set up the Player following the instructions given earlier in the thread. Give the Empty the partial rotation with an IPO extend mode of “Constant”. You need to know the frame numbers of the first and last frames of the rotation animation. For this example, I will say it starts at frame 1 and ends at frame 51. Add two properties, frame and oldY, both floats. Set frame to the first frame (1.000 for this example). Now set up the logic bricks as follows:
Mouse(Movement) >> Python(mouseMoveVert.py)>>IPO(Property:frame)
Here is the code for mouseMoveVert.py: (It’s pretty much the same as the horizontal one, except you need to test keep the frame value between the first frame and the last frame.)
cont = GameLogic.getCurrentController()
own = cont.getOwner()
move = cont.getSensor("<i>type your mouse move sensor name here</i>")
anim = cont.getActuator("<i>type your IPO actuator name here</i>")
firstFrame = 1
lastFrame = 51
movement = move.getYPosition()
scale = 1
# change the scale factor to speed up or slow down the rotation
# use the following "if" statements to test the value of frame
# and keep it in the range
if own.frame >= firstFrame:
if own.frame <= lastFrame:
own.frame -= own.oldY - (movement*scale)
own.oldY = movement
# now if it gets outside the range, we need to bring it back in
if own.frame < firstFrame:
own.frame = firstFrame
if own.frame > lastFrame:
own.frame = lastFrame
# the frame is set, run the IPO actuator
GameLogic.addActiveActuator(anim,1)
Just as a final tip, it’s a good programming practice to use variables like “firstFrame” and “lastFrame” instead of the actual numbers. That way if you change anything later, you’ll only have to change the values at the top of the script instead of digging through the whole thing and changing every occurence of the numbers.
Let me know if you have any trouble implementing this.
Alright thanks again…hope this is helping abel too…I’m trying now:D
Oh. Oops. That whole vertex parent setup is for rotating around a third-person view (what I was doing the other day). Skip the the vertex parent and cut out Empty1. Just put the horizontal rotation on the player and normal parent an empty to it with the vertical rotation. I’m going to edit my last post to reflect that.
lol ya i was thinking of ignoring that vertex parent thing for now anyway:p…cool now im even less confused:D
When I get home, I’ll PM you the project that my team is working on if you’d like (I don’t want to post a link in the forum because we are not really ready to release it yet, and we may or may not be publishing it commercially). I think you’ll be able to see how the vertex parenting comes into play. Basically, it allows the camera to rotate without rotating the Player. (Of course, in an FPS you want the player to rotate with the camera, so the vertex parenting would be counter-productive.) I also set up a neat camera zoom function that zooms with the mouse wheel or the +/- keys.
cool that’d be great…the script doesn’t seem to work i think im doing something wrong:confused:…Should all the things with X values be Y’s?
Yeah. You’re right, they should be Y’s. I just cut and pasted the script from the first post and forgot to change it. That’s good that you caught that. I think you’re starting to get a handle on this Python stuff. I’ll change the post above to reflect that.