Few tricks to make the rocket feel nicer (with an acceleration)

Hi it’s my first time to this forum and I wanna share a couple of changes I made so that the rocket manipulation can feel nicer, perhaps? One approach is to add acceleration to throttle and both acceleration and deceleration to rotation. I also used raycast to make player not only touching landing spot but also stopping quite well to pass the level.
testplayrocket
Here’s my code. What do you guys think? Feel free to copy and try them out.

   private void TrottleInput()
    {
        if (Input.GetKey(KeyCode.Space) && ySpeed <= maxSpeed)
        {
            // trottle
            //myRigidBody.AddRelativeForce(Vector3.up);

            //// v2
            ySpeed = transform.InverseTransformDirection(myRigidBody.velocity).y + (acceleration * Time.deltaTime);
            myRigidBody.AddRelativeForce(Vector3.up * (acceleration * Time.deltaTime));

            //// try change velocity directly
            //Vector3 velocityOnOwnAxis = transform.InverseTransformDirection(myRigidBody.velocity);
            //ySpeed = velocityOnOwnAxis.y + acceleration * Time.deltaTime;
            //velocityOnOwnAxis.y = ySpeed;
            //myRigidBody.velocity = transform.TransformDirection(velocityOnOwnAxis);
        }
        else
        {
            // update speed
            ySpeed = transform.InverseTransformDirection(myRigidBody.velocity).y;

        }

    }

   private void Rotation()
    {
        // remove rotation due to physics
        myRigidBody.angularVelocity = Vector3.zero;
        //rotate
        if (Input.GetKey(KeyCode.A) && curRotationSpeed <= maxRotationSpeed)
        {
            //rotate left
            curRotationSpeed += rotationAcceleration * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.D) && curRotationSpeed >= -maxRotationSpeed)
        {
            // rotate right
            curRotationSpeed -= rotationAcceleration * Time.deltaTime;
        }
        else
        {
            if (curRotationSpeed > rotationDecceleration * Time.deltaTime)
            {
                curRotationSpeed -= rotationDecceleration * Time.deltaTime;
            }
            else if (curRotationSpeed < -rotationDecceleration * Time.deltaTime)
            {
                curRotationSpeed += rotationDecceleration * Time.deltaTime;
            }
            else
            {
                curRotationSpeed = 0f;
            }
        }
        transform.Rotate(Vector3.forward * curRotationSpeed);
    }

   public bool IsLanded()
    {
        int landingLayerMask = LayerMask.GetMask("Landing");
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, landingDetectRange, landingLayerMask)
            && Mathf.Abs(ySpeed)<0.001)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
1 Like

Nice code, the acceleration can be a great addition.

A little feedback, I think you are overcomplicating things code wise, I’m telling you this because you are using raycast, something I consider quite advance for this section, so I’m assuming you have more experience.

The acceleration, even tho is great, can be achieved without all those variables, just read the input axis; Input.GetAxis(“Whaterever your axis is called”), this will fake an acceleration that can be tweaked in the Player Settings - Input Manager.

Your “IsLanded” method can be also achieved way easier, you don’t need a raycast, you can check for the ships rotation, or even simplier, a collider below the ship and then check for the speed.

Don’t get me wrong, your code is great, but try to using less code and “more Unity”, this is something I learned the hard way, Unity’s basic options are design to let you do more with less code, this is great for flexibility, debugging and also performance.

1 Like

Thanks for your reply. You’re right. As a beginner of Unity, I tend to over-code things. To examine rotation is a good idea for landing since ray is not casting out each frame in that case.