Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion 2D SpaceShip Rotation (HELP NEEDED)

Discussion in 'Scripting' started by nirmaljake, Jan 18, 2023.

  1. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    59
    I need help this is my game where a ship floats in space and it can go up and down, but I want a slight rotation whenever it goes up and down does anyone know how to achieve this effect, it is somewhat similar to the timing missile brackeys showed. upload_2023-1-18_15-29-17.png
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    You rotate the space ship's transform. How exactly depends on your setup, e.g. if the rotation needs to be part of an existing animation or if it needs to be done by code. Or if you rotate the root transform of your space ship or only a child transform.

    I suggest you look at your existing space ship controller and how exactly it moves and animates the ship. When you're able to understand what it's doing, it should become more clear where to add the rotation or you can ask another question with more specific details.
     
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    I did exactly this a few months ago for a game jam!

    Code (CSharp):
    1. float tilt = -45f * MathfExt.Logistic(velocity.x / speed, 2f);
    2. transform.rotation = Quaternion.AngleAxis(tilt, Vector3.forward);
    Note that the Logistic function is something custom. It's not from Mathf. I used it to get lots of tilt at low speeds with a smooth trail-off as speeds went up.

    The general idea, though, is to take your speed and turn it into an angle. So, maybe you could just do this:

    Code (CSharp):
    1. float tiltFactor = Mathf.InverseLerp(-5, 5, verticalSpeed);
    2. float tilt = Mathf.Lerp(-10, 10, tiltFactor);
    3. transform.rotation = Quaternion.AngleAxis(tilt, Vector3.forward);
    (this might rotate the wrong way; i can never remember if it's clockwise or counterclockwise)
     
  4. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    59
    Well the ships only function is up or down, and it just looks really nooby, without any rotation, and as I mentioned I needed something similar to brackey's homing missile video, so if I could get some rotation like that it would be good. I don't need an animation, I need the ship to rotate up or down whenever it goes up or down!
     
  5. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    59
    Well where would I add this, I have my player Script right here, and I need to know should I add this in the update function or add this in your new logistics function? + I'm 12 years old, I kinda don't fully know C# yet!
     
  6. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    59
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.  
    9.     public float moveSpeed = 5f;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         float moveDirection = Input.GetAxisRaw("Vertical");
    21.         rb.velocity = new Vector2(0, moveDirection * moveSpeed);
    22.     }
    23.  
    24.     void OnTriggerEnter2D(Collider2D other)
    25.     {
    26.         if(other.gameObject.CompareTag("Obstacle"))
    27.         {
    28.             Destroy(gameObject);
    29.         }
    30.     }
    31. }
    32.  
     
  7. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    59
    Also should I use my moveSpeed variable or should I create a new one called verticalSpeed?
     
  8. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    Your
    moveSpeed
    variable is your maximum speed. You want your current speed (in the vertical axis, specifically).

    rb.velocity
    tells you both where you're going and how fast you're going. So, after one second, if your position used to be
    pos
    , your position would become
    pos + rb.velocity
    .

    rb.velocity.y
    tells you how fast you're going in the y-axis in particular, which is what you want.
     
  9. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    59
    Although this is now working, it's kind of choppy, but it's better than nothing!
    Thanks for the Help Chemical!