- Home /
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.
If you crosspost to the forums, please link to the other location as well, so efforts aren't duplicated. http://forum.unity3d.com/threads/85837-Double-Dragon-style-Camera-and-movement
Answer by FrHaYwOrKs · Apr 14, 2011 at 03:24 PM
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 movement i wanted to use a old style movement. Because the older games used sprites, they could move more then 180 degrees, and back. So thats kinda what i want to recreate. I want them to move to where they are looking, but only on a straight line. So kinda like a 2D sidescroller, yet the movement is not restricted to just 2 axes, only the rotation is restricted.
I must admit I have no idea on how this could be accomplished right now... :(
Thanks for the camera script btw, works like a charm!
Answer by loopyllama · Apr 15, 2011 at 08:19 AM
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.
When i set the rotation to 180 degrees, he seems to be doing this continuously as long as i'm holding down a direction. So when i'm holding down left, he keeps turning like a maniac :D. Any suggestions?
yeah make sure you are only checking the left right dimension, be it x or z, and base your rotation on only that dimension. also, post the relevant code if that doesn't work...
Answer by Aurelio Provedo · Apr 15, 2011 at 07:54 AM
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.
Answer by SandjeMandje · Apr 16, 2011 at 09:55 AM
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!