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.
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.
I did exactly this a few months ago for a game jam!
float tilt = -45f * MathfExt.Logistic(velocity.x / speed, 2f);
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:
float tiltFactor = Mathf.InverseLerp(-5, 5, verticalSpeed);
float tilt = Mathf.Lerp(-10, 10, tiltFactor);
transform.rotation = Quaternion.AngleAxis(tilt, Vector3.forward);
(this might rotate the wrong way; i can never remember if it’s clockwise or counterclockwise)
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!
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!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody2D rb;
public float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float moveDirection = Input.GetAxisRaw("Vertical");
rb.velocity = new Vector2(0, moveDirection * moveSpeed);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Obstacle"))
{
Destroy(gameObject);
}
}
}
Also should I use my moveSpeed variable or should I create a new one called verticalSpeed?
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.
Although this is now working, it’s kind of choppy, but it’s better than nothing!
Thanks for the Help Chemical!