Double Dragon style Camera and movement

Hello guys, hope you can help me. I'm making an old arcade style game, where the character walks on 2 axes, the x and z, and jumps up. The movement works fine now. I can walk left of the screen and to the right and up and down the street.

The problem is however, that i want my character to rotate to the left (180 degrees) and back when i go to the right (kinda like strafing up and down, so that the rotation is always 180 degrees or 0 when walking the other way). When i use the scripts i found on the web, the character just rotates to the direction i'm walking toward, wich is not what i want.

The other problem i'm having, is that when i rotate the charater, the camera (attached to the player character) rotates with it.

Help is really appreciated guys.

For what matters the first problem I don't really understand what you mean, why wouldn't you have the character rotating where you are walking to? Where should it rotate instead?

For what matters the camera problem, maybe you should use a script on the camera itself to follow your character, instead of linking it to the moving objects, since it can rotate and it would be harder to re-rotate the camera every time!

Try something to follows the object... like:

``````var character : Transform;
var offset : Vector3 = Vector3(0, 0, -10);

function LateUpdate () {
var newPosition = Vector3(character.position.x, character.position.y, character.position.z);
transform.position = newPosition + offset;
}

``````

And then set up the offset to what you like more to follow the character from the right camera position!

For the rotation, just create a temporary Vector3 and assign it the character's speed, then set its Y to zero, and use this vector for rotation. Something like this:

``````var rotateDir = mySpeed; // Or myDirection, whatever you're using.
rotateDir.y = 0;
rotateDir.Normalize();
if ( rotateDir.sqrMagnitude > 0.01)
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (rotateDir), Time.deltaTime * rotationSmoothing); // rotationSmoothing should be an attribute of the script.

``````

For the camera control, I advice you to look at the 2d platformer tutorial, if you haven't done so already. I believe you could just take the camera scripts there and put them in your game, it should work just fine. You'll find more useful stuff in that tutorial, too.

When you update the position, store it in a variable called something like lastPos at the very end of the update. That way you can grab the Mathf.Sign of transform.position.x - oldPos.x. That is your left/right direction, a 1 or -1. If you are using sprite, just flip the uvs. Then you do not have to worry about the camera since you are editing uvs. If it is a 3d character set the transform.eulerAngles.y to 180 if the sign is -1 or 0 if the sign is 1. For the camera rotation problem if it a 3d character..you probably have your camera as a child of the player. Do not do this. Unparent the camera and character. Make the camera's x and y transform.position = character's transform.position. Leave the z position of the camera alone.

Fixed the movement now. I used a Tutorial for a previous style movement. When i was trying this, i forgot that i needed to remove the line "moveDirection = transform.TransformDirection(moveDirection);". So that line was transforming my direction when ever my rotation was different. D'oh.

For the controlls i'm using this now:

``````  if (transform.position.x < lastPos){
transform.eulerAngles = Vector3(0,180,0);
lastPos = transform.position.x;
}

if (transform.position.x > lastPos)
{
transform.eulerAngles = Vector3(0,0,0);
lastPos = transform.position.x;
}

``````

So that fixes it. Thanks every one!