Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a rocket pivot around it's bottom part insted of the middle in unity?

How would I go about making a rocket player controller rotate around its bottom part?

Heres my code up until now (I know it has problems, it's just a proof of concept for now)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rocket : MonoBehaviour
{
    Rigidbody rigidBody;
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
    }

    void Update()
    {
        ProcessInput();
    }

    private void ProcessInput()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            rigidBody.AddRelativeForce(Vector3.up);
        }

        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            if (!Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.RightArrow))
            {
                transform.Rotate(new Vector3(0, 0, 2));
            }
        }

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.LeftArrow))
            {
                transform.Rotate(new Vector3(0, 0, -2));
            }
        }
    }
}
like image 994
Yuval Amir Avatar asked Dec 01 '25 09:12

Yuval Amir


1 Answers

Good news, Unity has

https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

but! more usefully, it has:

https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html

I'm pretty sure that's exactly what you want.

(BTW be aware - it's not that trivial to figure out just what the vector you rotate around will be. You have to have a really good feel for cross products and other Vector manipulation. Unity is annoying like that - it seems really easy to use at first, but you really need a superb grasp of physics (I mean Newtonian physics) and every aspect of vectors and quaternions to really be able to make games - it's tough.)

One tip: you see how you are adding force to move it. Try adding torque (AddTorque) to turn it!

like image 194
Fattie Avatar answered Dec 04 '25 00:12

Fattie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!