Posted at 8:58 am , on December 29, 2017
I use Rigidbodies on the player objects in 1st & 10 Football but I have them set to Kinematic. When you have a Rigidbody set to Kinematic you cannot access the gameObject.transform.velocity property, it will give you an error. I needed to calculate the Velocity myself and thought I would post a quick bit of code in case it might be useful to someone else.
It’s pretty straightforward to calculate Velocity. Inside of your Update method you need to store the current position of the player, calculate the new position, and then subtract the two and divide it by deltaTime. How you decide to store the Velocity for use elsewhere is up to you, I store it in a property on the Player object outside of the movement FSM where the calculation takes place.
public Vector3 previousPosition = Vector3.zero;
public void Update()
previousPosition = player.transform.position;
player.transform.position = Vector3.MoveTowards(player.transform.position, destination, step);
player.Velocity = (player.transform.position - previousPosition) / Time.deltaTime;
Posted at 5:37 pm , on December 12, 2017
Currently, in Unity, I have my MainCamera rotated at an angle to the ground. If I use the Translate() method on the camera’s transform object, it moves in local space meaning if I move it in the Z-axis, it moves closer or farther away to the ground at an angle.
What I wanted, was to move parallel to the ground on the Z-axis when using the W & S keys, and move in the X-axis when using the A & D keys even though my camera is rotated. I knew Global space would allow me to do that but I couldn’t easily find how to affect the transform global space instead of in local space. After some Googling & tinkering, I bore the fruit of my labors with this script attached to my MainCamera (ignore the TODO & NOTE).
I have a custom input defined in Project Settings -> Inputs called “CameraHeight” that is mapped to the Q & E keys. That moves the camera straight up in the Y-axis. It seems that modifying the transform.position value affects the Global space coordinates. Using the Translate() method affects the camera’s transform in local space.
Thought this might be helpful to someone else, I had a hard time finding the answer in my search.