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. Dismiss Notice

Question 2D Space Shooter Movement

Discussion in 'Scripting' started by Zefrus, Jan 9, 2021.

  1. Zefrus

    Zefrus

    Joined:
    Dec 3, 2019
    Posts:
    14
    Still a beginner with coding. for context my project is 2D Top down, with the player locked on the y-axis and can only move left or right on the x-axis. I'm having trouble trying to figure out movement. There are two main objects, the player model and a crosshair that sits in front of the player. I'm trying to accomplish what Star Fox does in 3D, but in a 2D space. Where the player ship will move towards the crosshair directionally, but the crosshair will snap back to being in front of the player when the joystick is in the neutral position.

    So it's sort of a feedback loop:
    • Crosshair Movement
      • Default Location is in front of current player location
      • Moving left or right with joystick, crosshair snaps back to default position when in neutral
    • Player Ship Movement
      • Locked to x-axis
      • Player model will point towards crosshair and will rotate player model on y-axis
      • Player ship will move towards crosshair location
    I currently have some basic movement that feels really good that a tutorial helped me achieve. It was a 3D tutorial that I fitted for a 2D game.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     private Transform playerModel;
    9.     public bool joystick = true;
    10.     public float xSpeed = 100f;
    11.     Vector3 pos;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         playerModel = transform.GetChild(0);
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         float h = joystick ? Input.GetAxis("Horizontal") : Input.GetAxis("Mouse X");
    23.         float v = 0f;
    24.         LocalMove(h, v, xSpeed);
    25.         ClampPosition();
    26.     }
    27.  
    28.     void LocalMove(float x, float y, float speed)
    29.     {
    30.         transform.localPosition += new Vector3(x, y, 0) * speed * Time.deltaTime;
    31.     }
    32.  
    33.     void ClampPosition()
    34.     {
    35.         pos = Camera.main.WorldToViewportPoint(transform.position);
    36.         pos.x = Mathf.Clamp01(pos.x);
    37.         pos.y = Mathf.Clamp01(pos.y);
    38.         transform.position = Camera.main.ViewportToWorldPoint(pos);
    39.     }
    40. }
    41.  
    I have a target object (which will become the crosshair) with the same block of code for the moment. What I am struggling to achieve is re-fitting this code to allow for the crosshair movement as detailed above and have the player model follow that crosshair with a slight delay and have the crosshair snap back.

    I'm not asking anyone to solve this, but if someone could please point me in the right direction, it would be greatly appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Just to clarify, if I understand you correctly, you want:

    - the player moves the crosshairs in 2D (x and y) with mouse or joystick
    - the player ship aims (by rotation onscreen) towards the crosshairs
    - the player ship stays along the bottom edge always, gradually moving to line up with the crosshairs.

    If so, break it into those individual steps and implement one at a time so you can see where the issues are.
     
  3. Zefrus

    Zefrus

    Joined:
    Dec 3, 2019
    Posts:
    14
    Yeah that's the goal. Right, I think I'm getting confused because I'm trying to implement it all at once as opposed to set steps. I'll work on approaching it that way and see if it helps.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    If I were doing it, I would code up just step 1 and get that working.

    From the output of that crosshair, without looking at ANY more user input, then I would work on step 2 and step 3.

    For step 2 (rotation), you can use
    Mathf.Atan2()
    and
    Mathf.Rad2Deg
    to obtain a degrees heading from your ship to your crosshairs.

    You can feed that degrees heading directly into the player ship, or you can use
    Mathf.MoveTowardsAngle()
    to make it gradually move towards it.

    Here's a cheesy use of all those three Mathf functions in a little missile chase game.
     

    Attached Files:

  5. Zefrus

    Zefrus

    Joined:
    Dec 3, 2019
    Posts:
    14
    I'll check this out, thank you for your advice!
     
    Kurt-Dekker likes this.