Be the first to post for 'Resolving Movement Bugs'!

Here’s a little gif of my wizard flying on his magic broom :slight_smile:

2 Likes

A Rocket Launch

Redefining “On the edge…”

on a serious not though, I realized that @ben’s freezeRotation code was putting me right back in the position of having my ship rotate all sorts of weird ways, culminating in me trying to control a ship that was laying on its side pointed at the camera and not moving no matter how I rotated or thrusted.

My code (solution to the above is near the bottom

using UnityEngine;

namespace PB.Control
{
    public class Rocket : MonoBehaviour
    {
        float fuelLoad;                         // = seconds of burn time
        Vector3 RCSThrustVector;

        Rigidbody rocketRB;
        AudioSource audioSource;

        const float forceG = 0.00000000006f;    // Gravitational Constant (m/s/s)
        const float mainEngineThrust = 15f;     // Vector3.up default units (BU?)
                                                //   default units for "reletiveForce" not listed 
        const float RCSThrust = 30f;            // Vector3.up default units (deg?)
                                                //   defai;t units for "rotate" not listed

        void Start()
        {
            audioSource = GetComponent<AudioSource>();
            rocketRB = GetComponent<Rigidbody>();
            fuelLoad = 100;
        }

        void Update()
        {
            RCSThrustVector = Vector3.forward * RCSThrust * Time.deltaTime;
            GetInput();
        }

        private void GetInput()
        {
            if  (Input.GetKey("space"))             { MainEngineThrust(); }
                else                                { audioSource.Stop(); }

            if  (Input.GetKey("left"))              { FireRCSCounterClockwise(); }
                else if (Input.GetKey("right"))     { FireRCSClockwise(); }
        }


        private void MainEngineThrust()
        {
            if (!audioSource.isPlaying) { audioSource.Play(); }

            rocketRB.AddRelativeForce(Vector3.up * mainEngineThrust);
            fuelLoad = fuelLoad - Time.deltaTime;

            // Debug messages
            print("Main Engine Active.");
            if (audioSource.isPlaying) { print("audio on"); }
        }

        private void FireRCSCounterClockwise()
        {
            rocketRB.freezeRotation = true;
            transform.Rotate(RCSThrustVector);
            ReturnControlToAI();

            // Debug messages
            print("OMS Active, Roll Left.");
        }

        private void FireRCSClockwise()
        {
            rocketRB.freezeRotation = true;
            transform.Rotate(-RCSThrustVector);
            ReturnControlToAI();

            // Debug messages
            print("OMS Active, Roll Right.");
        }

        private void ReturnControlToAI()
        {
            rocketRB.constraints =
            RigidbodyConstraints.FreezeRotationX |
            RigidbodyConstraints.FreezeRotationY |
            RigidbodyConstraints.FreezePositionZ;
        }
    }
}

I’ll add the video tomorrow

Updated Wed Jul 24 2019 10:42
Still wasn’t quite happy with the transform.Rotate commands so did some searching in the docs and came up with this

using UnityEngine;

        Vector3 leftRCSThrust;
        Vector3 rightRCSThrust;
  
        void Start() {...}

        void Update() {...}

        private void GetInput() {...}

        private void MainEngineThrust() {...}

        private void FireRCSCounterClockwise()
        {
            rocketRB.AddRelativeTorque(leftRCSThrust);
        }

        private void FireRCSClockwise()
        {
            rocketRB.AddRelativeTorque(rightRCSThrust);
        }

and to complete the challenge my 7 1/2 minute video (20 min just seemed excessive)

Lol, not exactly groundbreaking, but I like it anyway =P

I had to add a collider for not to rotate with obstacles.

Everyone’s uploading videos, yet gaemdev tells me ‘Upload’ only accepts file types of: jpg, jpge, png, or gif
That sucks.

End of Lesson 57 where we did Invoke
Didn’t stray too much from the lesson’s visuals, but I did push all the key elements to as close of the same Z-axis as I could so the “faces” of the obstacles were as close to the actual hitbox area as I could manage. I know there is z-fighting, but I’ll worry about that later.

With my ship being lopsided I had to look up how to fix the bug where unfreezing the rotation would cause it to fall and change it’s z-axis. Also, didn’t notice the next lesson was going to handle multiple-sounds and I had looked up how to do that between lessons.
2020-05-27_22-21-17

Privacy & Terms