Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by seppukustl · Jul 26, 2013 at 12:28 PM · 2dmovementside-scrolling

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image seppukustl · Jul 27, 2013 at 05:14 PM 0
Share

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

avatar image seppukustl · Aug 01, 2013 at 04:34 PM 0
Share

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

avatar image Peter G · Aug 01, 2013 at 05:50 PM 0
Share

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.

avatar image seppukustl · Aug 01, 2013 at 06:44 PM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image seppukustl · Aug 01, 2013 at 05:46 PM 0
Share

I'm using an orthographic camera :(

avatar image seppukustl · Aug 01, 2013 at 05:51 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

wasd movement rigidbody no bouncing 0 Answers

Gravity switch 1 Answer

Errors when using multiple if's in a statement. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges