I have a pair of thrusters with (for lack of a better term) flappy things on them, and I want them to move in relation to the players velocity. In order to do this, I’ve pulled some code out of the AeroplaneControlSurfaceAnimator from the Standard Asset’s sample scenes and bastardized it to fit my needs.
I don’t fully understand most of what’s happening in there, but for the most part it works the way I’d like. Except I just noticed that my console is constantly spitting out “UnityEngine.Transform.get_localRotation () <0x1983c30a460 + 0x00072> in <07acd42a4d064cbdac11856405cd71fb>:0” and I’m not exactly sure why.
Here is my bloody and beaten script if anyone can explain to me what’s going on and where it’s getting this null from.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationController : MonoBehaviour
{
[SerializeField] private float m_Smoothing = 5f; // The smoothing applied to the movement of control surfaces.
[SerializeField] private ControlSurface[] m_ControlSurfaces; // Collection of control surfaces.
private Rigidbody player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<Rigidbody>();
foreach (var surface in m_ControlSurfaces)
{
surface.originalLocalRotation = surface.transform.localRotation;
}
}
private void Update()
{
foreach (var surface in m_ControlSurfaces)
{
switch (surface.type)
{
case ControlSurface.Type.Elevator:
{
// Elevators rotate negatively around the x axis, according to the plane's pitch input
Quaternion rotation = Quaternion.Euler(0f, 0f, surface.amount * player.velocity.z);
RotateSurface(surface, rotation);
break;
}
case ControlSurface.Type.Rudder:
{
// Rudders rotate around their y axis, according to the plane's yaw input
Quaternion rotation = Quaternion.Euler(0f, surface.amount * player.velocity.z, 0f);
RotateSurface(surface, rotation);
break;
}
}
}
}
private void RotateSurface(ControlSurface surface, Quaternion rotation)
{
// Create a target which is the surface's original rotation, rotated by the input.
Quaternion target = surface.originalLocalRotation * rotation;
// Slerp the surface's rotation towards the target rotation.
surface.transform.localRotation = Quaternion.Slerp(surface.transform.localRotation, target, m_Smoothing * Time.deltaTime);
}
// This class presents a nice custom structure in which to define each of the plane's contol surfaces to animate.
// They show up in the inspector as an array.
[Serializable]
public class ControlSurface // Control surfaces represent the different flaps of the aeroplane.
{
public enum Type // Flaps differ in position and rotation and are represented by different types.
{
Elevator, // Horizontal flaps used to adjusting the pitch of a plane, rotate on the x axis.
Rudder, // Vertical flaps on the tail, rotate on the y axis.
}
public Transform transform; // The transform of the control surface.
public float amount; // The amount by which they can rotate.
public Type type; // The type of control surface.
[HideInInspector] public Quaternion originalLocalRotation; // The rotation of the surface at the start.
}
}