- Home /
Strange Z axis movement in 2d sidescroller??
So I've been working on a 2D sidescrolling shooter for a while now and I've come across a snag. For some reason after implementing a "mouse aim" script to rotate the players firearm to the position of the mouse it tends to allow the player to move on the Z axis when the weapon is pointed in certain angles. I cannot for the life of me figure out how and why it is doing this. I also recently added a weapon swapping script that when switching to melee and deactivating the firearm with the "mouse aim" script it won't allow me to move forward. o.O
Any help would be greatly appreciated here are the three scripts that I mentioned in the above paragraph.
Movement Script
var walkSpeed : float = 2.0;
var runSpeed : float = 4.0;
var fallSpeed : float = 2.0;
var gravity : int = 20.0;
var Jump : int = 0.0;
var moveDirection : int = 1;
private var velocity : Vector3 = Vector3.zero;
function Update ()
{
var controller : CharacterController = GetComponent ( CharacterController );
if ( controller.isGrounded )
{
velocity = Vector3 ( Input.GetAxis ( "Horizontal" ), 0, 0 );
if ( velocity.x == 0 && moveDirection == 1 ) //idle right
{
//animation goes here
}
if ( velocity.x == 0 && moveDirection == 0 ) //idle left
{
//animation goes here
}
if ( velocity.x < 0 ) //walk left
{
velocity *= walkSpeed;
//animation goes here
}
if ( velocity.x > 0 ) //walk right
{
velocity *= walkSpeed;
//animation goes here
}
if ( velocity.x < 0 && Input.GetKey("left shift")) //run left
{
velocity *= runSpeed;
//animation goes here
}
if ( velocity.x > 0 && Input.GetKey("left shift")) //runright
{
velocity *= runSpeed;
//animation goes here
}
}
if ( !controller.isGrounded )
{
velocity.x = Input.GetAxis ( "Horizontal" );
velocity.x *= walkSpeed;
}
if ( velocity.x < 0 ) //last move direction left
{
moveDirection = 0;
}
if ( velocity.x > 0 ) //last move direction right
{
moveDirection = 1;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move ( velocity * Time.deltaTime );
}
Mouse Look Script
var mouse_pos : Vector3;
var target : Transform; //Assign to the object you want to rotate
var object_pos : Vector3;
var angle : float;
function Update ()
{
mouse_pos = Input.mousePosition;
mouse_pos.z = 0; //The distance between the camera and object
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
}
Weapon Swap Script
var Weapon01 : GameObject;
var Weapon02 : GameObject;
function Update () {
if (Input.GetKeyDown (KeyCode.Q))
{
SwapWeapons();
}
}
function SwapWeapons()
{
if (Weapon01.active == true)
{
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(true);
}
else
{
Weapon01.SetActiveRecursively(true);
Weapon02.SetActiveRecursively(false);
}
}
As stated any help would be greatly appreciated, I'm stumped :P
Answer by Olgo · Jul 26, 2013 at 01:20 PM
hello, i've had a similar problem (i think) when working on a 2D platformer. Seemed like the further I moved my player, the deeper in to the z axis he would move. I fixed this by adding a line in my character control script that always set his position to 0 for the z coordinate. works well.
something like this in Update:
this.transform.z = 0;
Thanks! I'll have to try this when I get a chance. Appreciate the input! Now to tackle the immobility upon weapon swapping issue. lol
I just got around to trying it and this is the code I placed in my update function.
transform.position.z+=0;
and I'm still getting the issue :(
On the plus side if figured out what was messing with the forward movement of my player upon switching weapons. The is trigger was not ticked on my collider xD
You don't want to use +=
. That will increment the value by nothing (so basically do nothing). Just use straight up assignment like @Olgo did in his answer.
awesome! I just changed it to transform.position.z = 0; and so far haven't had a problem, its a bit jittery on the above view but I doubt that will effect gameplay :D Thank you guys so much!
Answer by Seizure · Aug 01, 2013 at 04:48 PM
Try orthographic camera rather then perspective, it will make it so when 3d objects move to the far left or right of your screen you do not have a side aspect it will always look straight on.
I'm thinking it has to be something in the "mouse aim" script that is messing with my position because I don't believe I was having these problems before I implemented into the game.