Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

2D movement issue

Discussion in '2D' started by Velase85, May 30, 2020.

  1. Velase85

    Velase85

    Joined:
    Dec 8, 2014
    Posts:
    4
    Hello, I know i'm probably making this more complicated then it needs to be, but here is the issue:

    I want to make a player move with a bit of a weird control scheme. Two vertical axis joysticks used in conjunction for movement and rotation. (using 2D engine right now for simplicity)

    Right now I have two child objects with rigid bodies and each move according to the corresponding joystick without issue. My issue is I can't seem to get them to control the parent no matter how i try, most of the results are the left stick controlling the parent while the right stick still controls the right side child.

    for input I have in its own get input method:
    Code (CSharp):
    1. leftStickInput = new Vector2(0, Input.GetAxis("Vertical"));
    2. rightStickInput = new Vector2(0, Input.GetAxis("R_Vertical"))
    and base movement i have in fixed update:
    Code (CSharp):
    1. Vector2 leftMovement = leftStickInput * playerSpeed * Time.deltaTime;
    2. Vector2 rightMovement = rightStickInput * playerSpeed * Time.deltaTime
    in the fixed update i was also trying to move the rigidbody2D of each child, thinking this would be easier then putting a separate movement script on each child. Still debating on trying this last part, but either method still leaves me unable to control the parents object movement.

    any advice would be appreciated, thanks.
     
  2. Velase85

    Velase85

    Joined:
    Dec 8, 2014
    Posts:
    4
    Well, playing around I went and simplified it to this:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         rb = GetComponent<Rigidbody2D>();
    4.      }
    5.  
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.         GetPlayerInput();
    10.              
    11.     }
    12.  
    13.     private void GetPlayerInput()
    14.     {
    15.         leftStickInput = new Vector2(0, Input.GetAxis("Vertical"));
    16.         rightStickInput = new Vector2(0, Input.GetAxis("R_Vertical"));      
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         //move = leftStickInput + rightStickInput * playerSpeed * Time.deltaTime;
    22.         moveFoward = new Vector2(0, 1);
    23.         moveBackward = new Vector2(0, -1);
    24.  
    25.         if (Input.GetAxis("Vertical") == 1.0f && Input.GetAxis("R_Vertical") == 1.0f)
    26.         {
    27.             rb.MovePosition(rb.position + moveFoward);
    28.         }
    29.         else if (Input.GetAxis("Vertical") == -1.0f && Input.GetAxis("R_Vertical") == -1.0f)
    30.         {
    31.             rb.MovePosition(rb.position + moveBackward);
    32.         }
    33.          
    34.     }
    Now i just need to work out how to rotate CW & CCW based on the combination of those two float scale values while still applying movement.
    I know i can do something like
    Code (CSharp):
    1.  else if (Input.GetAxis("Vertical") >= 1.0f && Input.GetAxis("R_Vertical") <= 1.0f)
    2.         {
    3.             rb.rotation += -mfRot; //rotate clockwise
    4.         }
    for rotation but still need to figure out how to get the player moving in the corresponding direction otherwise it will only ever move linear on the vertical axis
     
    Last edited: May 30, 2020
  3. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    You need to post the code where you actually move the objects, the script in each object
     
  4. sol-erides

    sol-erides

    Joined:
    Dec 8, 2018
    Posts:
    17
    Could you post a picture or diagram of your character and how you expect it to move when the left/right joysticks are used independently? From what I can tell, you want the character to move forwards when both joysticks are pushed, but rotate to some extent when only one is being pushed - correct me if I got it wrong.

    Right now since each child is a separate physical object, they will move independently of each other. I haven't used this myself, but it might help with what you're trying to do: https://docs.unity3d.com/Manual/class-HingeJoint2D.html. More information would definitely help though.
     
  5. Velase85

    Velase85

    Joined:
    Dec 8, 2014
    Posts:
    4
    that is exactly right. I simplified it down to 1 object while trying to work thru the problem, but even using force or velocity instead of move position, it dosen't behave the way i'm aiming for. I will add some visual depictions soon as i get back to my main computer later today.
     
  6. Velase85

    Velase85

    Joined:
    Dec 8, 2014
    Posts:
    4
    This is one of the concepts I was using as a place holder, but think of the thrusters as separate engines almost like the nacelles you see on the enterprise. The idea being that each throttle controls a side for forward thrust, and counter thrust, this also being ideal way to rotate the ship (like a real life tank or excavator). I wanted the challenge of making a space shooter that did not have direct lateral movement input by the user, but I may add lateral thrusters that can be used on a cool down basis.
     

    Attached Files:

  7. sol-erides

    sol-erides

    Joined:
    Dec 8, 2018
    Posts:
    17
    Right, this paints a clear picture along with the diagram. Since you want the whole ship to behave like a single physical object (no hinges or rotating parts) you could try using the function RigidBody2D.AddForceAtPosition. Adding force from the position of each thruster/counter thruster should produce a rotational force which can be used to turn the ship.

    Docs: https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForceAtPosition.html

    I feel like it would be hard for a player to accurately position the ship using just this though, so you might have to add additional logic to help them control their rotation better - maybe lateral thrusters as you mentioned.