Skip to content

Instantly share code, notes, and snippets.

@TheBlackPlague
Last active December 9, 2018 22:18
Show Gist options
  • Save TheBlackPlague/1235a39a576eab1f2eb8a11d64ed1f4b to your computer and use it in GitHub Desktop.
Save TheBlackPlague/1235a39a576eab1f2eb8a11d64ed1f4b to your computer and use it in GitHub Desktop.
An example version of code for Movement of a Player in 3D within Unity using the Entity Component System [Keyword: ECS].
/**
MIT License
Copyright (c) 2018 Shaheryar Sohail
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Collections;
using UnityEngine;
using Unity.Entities;
public class PlayerInput : MonoBehaviour {
// Movement Values
public float i;
public float j;
public float k;
public float speed = 0.1f;
// Rotation Values
public float yRot;
public float xRot;
public float sensitivity = 3f;
public Quaternion turnRot;
public Quaternion camRot;
}
class PlayerInputSYS : ComponentSystem {
private struct Filter {
public PlayerInput input;
}
// Runs every frame just like Update()
protected override void OnUpdate() {
float inputI = Input.GetAxisRaw("Horizontal");
float inputJ = getJ();
float inputK = Input.GetAxisRaw("Vertical");
foreach (var entity in GetEntities<Filter>()) {
entity.input.i = inputI;
entity.input.j = inputJ;
entity.input.k = inputK;
entity.input.yRot = Input.GetAxisRaw("Mouse X");
entity.input.xRot = Input.GetAxisRaw("Mouse Y");
}
}
private float getJ() {
float j = 0;
if (isSpacePressed()) {
j = 1f;
}
return j;
}
private bool isSpacePressed() {
KeyCode space = KeyCode.Space;
bool inputPressed = Input.GetKeyDown(space);
if (inputPressed == true) {
return true;
}
return false;
}
}
class PlayerMovementSYS : ComponentSystem {
private struct Filter {
public Rigidbody rb;
public Transform tf;
public PlayerInput input;
}
// Runs every frame just like Update()
protected override void OnUpdate() {
float deltaTime = Time.deltaTime;
performMovement();
performTurnHorizontal();
}
/**
* Performs Movement.
*/
private void performMovement() {
foreach (var entity in GetEntities<Filter>()) {
Vector3 mVector = new Vector3(entity.input.i, entity.input.j, entity.input.k);
Vector3 newPosition = entity.rb.position + (entity.tf.up * mVector.y + entity.tf.right * mVector.x + entity.tf.forward * mVector.z) * entity.input.speed;
entity.rb.MovePosition(newPosition);
}
}
/**
* Turns the player on the Horizontal Axis.
*/
private void performTurnHorizontal() {
foreach (var entity in GetEntities<Filter>()) {
Vector3 rVector = new Vector3(0f, entity.input.yRot * entity.input.sensitivity, 0f);
Quaternion rotation = Quaternion.Euler(rVector);
Quaternion newRotation = entity.rb.rotation * rotation;
entity.rb.MoveRotation(newRotation);
entity.input.turnRot = newRotation;
}
}
}
class CameraMovementSYS : ComponentSystem {
private struct Filter {
public Camera cam;
public Transform tf;
public PlayerInput input;
}
protected override void OnUpdate() {
performCameraTurnVertical();
}
private void performCameraTurnVertical() {
foreach (var entity in GetEntities<Filter>()) {
Vector3 rVector = new Vector3(entity.input.xRot * entity.input.sensitivity, 0f, 0f);
Quaternion rotation = Quaternion.Euler(-rVector);
Quaternion newRotation = entity.tf.rotation * rotation;
entity.tf.rotation = newRotation;
entity.input.camRot = newRotation;
}
}
}
@TheBlackPlague
Copy link
Author

TheBlackPlague commented Dec 9, 2018

Requirements & Setup

You are required to have the camera be the child of the parent (PlayerObject) at a position of an FPS-based game (refer to games like CSGO for examples of how the camera position is relatively to Player Model).

Along with this, you're also required to have the RigidBody component on the Player along with the Transform component (which is there by default).

For camera based movement & rotation, please understand you must add the PlayerInput MonoBehaviour Script to the Player and Camera.

For non-camera based movement & rotation, please only add the PlayerInput MonoBehaviour Script to the Player.

License

This code (software) is licensed under the MIT License and you're welcome to use it as fit. Please read the license below:

MIT License

Copyright (c) 2018 Shaheryar Sohail

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Per the license, this software is PROVIDED without any support. However, you're welcome to comment and ask anything regarding it in the comments below and if someone - including me - feels like helping you out, they will. Do understand that nobody is under any obligation to keep this updated, this may be out-dated and you have no right to ask for update. If someone chooses to, you do have the right to thank them for it. 😃

Enjoy the code.

Follow me on Twitter so I can tweet to you. 🐦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment