I’ve simplified the rotation code I think in my challenge response, but I still think it’s problematic to freeze rotation during the rotate call. In my video you can see some weird things. I had also experimented with when to lock and unlock the freeze on the rotation and I got weird results there as well. It seemed that I could then rotate my ship past the constraints on the inspector when I hit the obstacles. I’m happy with the placeholders for now, I’ll be making something cooler in Blender later. Either way, here is the walking rocket ship video:
Here is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rocket : MonoBehaviour {
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 15f;
Rigidbody rigidBody;
AudioSource audioSource;
// Use this for initialization
void Start ()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
Thrust();
Rotate();
}
private void Thrust()
{
if (Input.GetKeyDown(KeyCode.Space))
{
audioSource.Play();
}
if (Input.GetKeyUp(KeyCode.Space))
{
audioSource.Stop();
}
if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust);
}
}
private void Rotate()
{
float rotationThisFrame = rcsThrust * Time.deltaTime;
float rotationDirection = 0f;
rigidBody.freezeRotation = true; // take manual control of rotation
if (Input.GetKey(KeyCode.A))
{
rotationDirection++;
}
if (Input.GetKey(KeyCode.D))
{
rotationDirection--;
}
transform.Rotate(Vector3.forward * rotationDirection * rotationThisFrame);
rigidBody.freezeRotation = false; // resume physics (return control)
}
}