Search Unity

2D Movement Help

Discussion in '2D' started by Smealiam, Apr 3, 2019.

  1. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    If anyone remembers MicroBattles on IOS, I'm trying to make a version on the PC of the space game. The ships rotate and the player presses the space bar to propel the ship. The direction in which it is propelled is based on the rotation of the ship when the button is pressed (the ship is constantly rotating until the player chooses to thrust, then the ship stops rotating and goes in whatever direction the front was facing). How would I get this into C-Sharp code? I'm not entirely sure how I would get the direction travel to be based on the rotation, I'm very new to coding and the game design teacher apparently doesn't know code either (I don't know why he's teaching?).

    Thanks.
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Are you using rigidbody or editing transforms
     
  3. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    2D Rigidbody

    The current code:

    public float speed;
    public bool spinning = true;
    private Rigidbody rb;
    void Start()
    {
    rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    rb.AddForce(movement * speed);
    if (Input.GetKey(KeyCode.Space))
    {
    spinning = false;
    }
    if (spinning == true)
    {
    transform.Rotate(new Vector3(0, 0, -30) * Time.deltaTime);
    }
    }

    }
     
  4. besomuk

    besomuk

    Joined:
    Jul 29, 2010
    Posts:
    80
    Add rigidbody to your object and this should work...

    Code (CSharp):
    1. public class RotateAndMove : MonoBehaviour
    2. {
    3.     public float rotateSpeed = 100;    // desired rotation speed
    4.     private float currentRotateSpeed;  // current rotation speed
    5.     public float moveSpeed = 100;      // move speed
    6.  
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         // check for movement, if moving, don't rotate
    11.         if ( gameObject.GetComponent<Rigidbody2D>().velocity.magnitude > 0.2f ) // or 0.0f :). Just check for zero movement....
    12.         {
    13.             currentRotateSpeed = 0;
    14.         }
    15.         else
    16.         {
    17.             currentRotateSpeed = rotateSpeed;
    18.         }
    19.  
    20.         transform.Rotate( new Vector3( 0, 0, 1 ) * Time.deltaTime * currentRotateSpeed ); // rotate on z axis
    21.  
    22.         if ( Input.GetKeyDown( KeyCode.A ) )
    23.         {
    24.  
    25.             gameObject.GetComponent<Rigidbody2D>().AddRelativeForce( new Vector2( 0, moveSpeed ) ); // add force on objects y axis
    26.         }
    27.     }
    28. }
     
  5. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    help.PNG
    Code (CSharp):
    1. //Looks like this
     
  6. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
     
  7. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    Thank you so much, works great!
     
  8. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    Also, another question: I've added four colliders/triggers around the screen, which will make the ship transform its position when it collides with one of them. I need to have the ship get transformed to the opposite position across from the edge (i.e. if the ship hits the bottom collider, it will come back from the top at the same position). The current code I have:

    using UnityEngine;
    using System.Collections;

    public class DeathRespawn : MonoBehaviour
    {
    [SerializeField] private Transform player;
    [SerializeField] private Transform teleport;

    void OnTriggerEnter(Collider col)
    {
    player.transform.position = teleport.transform.position;
    }
    }

    Thanks
     
  9. besomuk

    besomuk

    Joined:
    Jul 29, 2010
    Posts:
    80
    So, what's the question?

    You should have way to detect which collider you hit ( use tags ) and, based on that information, send object to teleport location...
     
  10. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    I've done that, however the ship just disappears when it collides.
     
  11. besomuk

    besomuk

    Joined:
    Jul 29, 2010
    Posts:
    80
    Does other collider exists where teleport target is? Is it possible that you, actually, teleport your object to other collider which teleports it back and so on?

    Anyway, other solution might be to check where your object is. If it's beyond screen edge ( pay attention on world/screen coordinates ! ) just send it to opposite side. Something like this, this will check object moving on left and chek if it's beyond left screen edge ( leftScreenlimit ).
    Note - it's in World space.

    Code (CSharp):
    1.     void CheckBorders()
    2.     {
    3.         if ( gameObject.transform.position.x < leftScreenLimit ) // am I beyond defined border on X?
    4.         {
    5.             Vector2 oldPosition = gameObject.transform.position;  // my old position
    6.             gameObject.transform.position = new Vector2( oldPosition.x * -1, oldPosition.y ); // multiply X by -1 to send it on other side, Y remains same
    7.         }
    8.     }
    This is code is just to show you basic idea...
     
  12. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    The code definitely works, the only question I have is what object I should put the script on? The actual player ship or an empty game object? Also, where's the best place to actually learn C-Sharp coding for this kind of stuff? My class has not really taught it.
     
    Last edited: Apr 12, 2019
  13. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    I've gotten this code, but it doesn't seem to be working, the ship just keeps going. Can you see anything wrong with it?
     
  14. besomuk

    besomuk

    Joined:
    Jul 29, 2010
    Posts:
    80
    What code :)
     
  15. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    @besomuk

    using UnityEngine;
    using System.Collections;

    public class Edges : MonoBehaviour
    {
    private const double V = 0.6;
    int leftScreenLimit = 17;
    int rightScreenLimit = 1;
    double topScreenLimit = V;
    double bottomScreenLimit = -V;
    public GameObject player;
    private void OnTriggerEnter2D(Collider2D collision)
    {

    {
    if (player.transform.position.x < leftScreenLimit) // am I beyond defined border on X?
    {
    Vector2 oldPosition = gameObject.transform.position; // my old position
    player.transform.position = new Vector2(oldPosition.x * -1, oldPosition.y); // multiply X by -1 to send it on other side, Y remains same
    }
    if (player.transform.position.x < rightScreenLimit) // am I beyond defined border on X?
    {
    Vector2 oldPosition = player.transform.position; // my old position
    player.transform.position = new Vector2(oldPosition.x * -1, oldPosition.y); // multiply X by -1 to send it on other side, Y remains same
    }
    if (player.transform.position.y < topScreenLimit) // am I beyond defined border on Y?
    {
    Vector2 oldPosition = player.transform.position; // my old position
    player.transform.position = new Vector2(oldPosition.y * -1, oldPosition.x); // multiply Y by -1 to send it on other side, X remains same
    }
    if (player.transform.position.y < bottomScreenLimit) // am I beyond defined border on Y?
    {
    Vector2 oldPosition = player.transform.position; // my old position
    player.transform.position = new Vector2(oldPosition.y * -1, oldPosition.x); // multiply Y by -1 to send it on other side, X remains same
    }
    }
    }
    }
     
  16. besomuk

    besomuk

    Joined:
    Jul 29, 2010
    Posts:
    80
    First, pay attention on your oldPosition variable. It's holding position of the object that has your script attached - not player object. oldPosition should take player position NOT gameObject position.

    Second, try with if ( player.transform.position.x > rightScreenLimit ), not if ( player.transform.position.x < rightScreenLimit )
    :)
     
  17. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    @besomuk

    I've added some rectangles around the sides and made them 2D triggers, so that the code will start when the ship hits it. Unfortunately, the ship just disappears after.
     
  18. Smealiam

    Smealiam

    Joined:
    Apr 3, 2019
    Posts:
    11
    using UnityEngine;
    using System.Collections;

    public class Edges : MonoBehaviour
    {
    private const double V = 0.6;
    int leftScreenLimit = 17;
    int rightScreenLimit = 1;
    double topScreenLimit = V;
    double bottomScreenLimit = -V;
    public GameObject player;
    private void Update ()
    {

    {
    if (player.transform.position.x < leftScreenLimit) // am I beyond defined border on X?
    {
    Vector2 oldPosition = player.transform.position; // my old position
    player.transform.position = new Vector2(oldPosition.x * -1, oldPosition.y); // multiply X by -1 to send it on other side, Y remains same
    }
    if (player.transform.position.x > rightScreenLimit) // am I beyond defined border on X?
    {
    Vector2 oldPosition = player.transform.position; // my old position
    player.transform.position = new Vector2(oldPosition.x * -1, oldPosition.y); // multiply X by -1 to send it on other side, Y remains same
    }
    if (player.transform.position.y < topScreenLimit) // am I beyond defined border on Y?
    {
    Vector2 oldPosition = player.transform.position; // my old position
    player.transform.position = new Vector2(oldPosition.y * -1, oldPosition.x); // multiply Y by -1 to send it on other side, X remains same
    }
    if (player.transform.position.y < bottomScreenLimit) // am I beyond defined border on Y?
    {
    Vector2 oldPosition = player.transform.position; // my old position
    player.transform.position = new Vector2(oldPosition.y * -1, oldPosition.x); // multiply Y by -1 to send it on other side, X remains same
    }
    }
    }
    }