How to move gameobject like Blockland? (SOLVED) Lateral Movement (Very Useful Script)

I so badly want to make a game with blockland features in Unity 5 c#. I kept searching for so many months. And now I finally played the Blockland game Tutorial; And found out the movements of Numpad keys of 8,4,6 and 2 move lateral? I google search for this but can’t find it for Unity 5 c#? Do you guys know how to make a GameObject move lateral with numpad keys 8,4,6 and 2 in Unity 5 c#?

Here is a example of the game of the game I was playing for references. Not my game some one else’s game.

(((Solved)))

heres the code:

public class MoveCubeFPSMovement : MonoBehaviour 
{
	[SerializeField] public float FPSCubeMovement = 1.0f;
	[SerializeField] public Transform target;
	[SerializeField] public float angle1 = 0.0f;
	[SerializeField] public float angle2 = 179.9f;

	private void Awake()
	{
		angle1 = 0.0f;
		angle2 = 179.9f;
	}

	void Update () 
	{
		Vector3 targetDir = target.position - transform.position;
		float angle1 = Vector3.Angle (targetDir, transform.forward);
		float angle2 = Vector3.Angle (targetDir, transform.right);


		if (Input.GetKeyDown (KeyCode.Keypad8)) 
		{
			if (angle1 >= 135.0f ) 
			{
				Debug.Log ("FPS cube moved forward");
				transform.position += transform.forward * FPSCubeMovement;
			}
			if (angle1 <= 45.0f ) 
			{
				Debug.Log ("FPS cube moved backward");
				transform.position -= transform.forward * FPSCubeMovement;
			}
			if (angle2 >= 135.0f ) 
			{
				Debug.Log ("FPS cube moved right");
				transform.position += transform.right * FPSCubeMovement;
			}
			if (angle2 <= 45.0f ) 
			{
				Debug.Log ("FPS cube moved left");
				transform.position -= transform.right * FPSCubeMovement;
			}
		}
		if (Input.GetKeyDown (KeyCode.Keypad2)) {
			if (angle1 >= 135.0f) {
				Debug.Log ("FPS cube moved forward");
				transform.position -= transform.forward * FPSCubeMovement;
			}
			if (angle1 <= 45.0f) {
				Debug.Log ("FPS cube moved backward");
				transform.position += transform.forward * FPSCubeMovement;
			}
			if (angle2 >= 135.0f) {
				Debug.Log ("FPS cube moved right");
				transform.position -= transform.right * FPSCubeMovement;
			}
			if (angle2 <= 45.0f) {
				Debug.Log ("FPS cube moved left");
				transform.position += transform.right * FPSCubeMovement;
			}
		}
		if (Input.GetKeyDown (KeyCode.Keypad4)) {
			if (angle1 >= 135.0f) {
				Debug.Log ("FPS cube moved forward");
				transform.position -= transform.right * FPSCubeMovement;
			}
			if (angle1 <= 45.0f) {
				Debug.Log ("FPS cube moved backward");
				transform.position += transform.right * FPSCubeMovement;
			}
			if (angle2 >= 135.0f) {
				Debug.Log ("FPS cube moved right");
				transform.position += transform.forward * FPSCubeMovement;
			}
			if (angle2 <= 45.0f) {
				Debug.Log ("FPS cube moved left");
				transform.position -= transform.forward * FPSCubeMovement;
			}
		}
		if (Input.GetKeyDown (KeyCode.Keypad6)) {
			if (angle1 >= 135.0f) {
				Debug.Log ("FPS cube moved forward");
				transform.position += transform.right * FPSCubeMovement;
			}
			if (angle1 <= 45.0f) {
				Debug.Log ("FPS cube moved backward");
				transform.position -= transform.right * FPSCubeMovement;
			}
			if (angle2 >= 135.0f) {
				Debug.Log ("FPS cube moved right");
				transform.position -= transform.forward * FPSCubeMovement;
			}
			if (angle2 <= 45.0f) {
				Debug.Log ("FPS cube moved left");
				transform.position += transform.forward * FPSCubeMovement;
			}
		}
	}
}

I forgot to add the explanation, I edited it on. Basically, when you pass in "this" as a parameter, it's an Extension. Otherwise it's just referring to the current class's instance. edit: Then I forgot to answer your immediate question: When "this" is used as a parameter while extending a class, then yes, it should be static. And this would go for all types. This is because you are not modifying the original class, but simply non-destructively extending it.

–

1 Answer

1

(Update! Old Don’t read this one! Not important! Read the comment on top that says (solved)
I have good news! I believe I just need to add a if statement angles for the player controller target reference. I am getting their! :'D I had this issue for a few years now. Here is the script I made for my cube to move in FPS camera view. This cube in my project moves forward with the 8 numpad key. And when I look in front of the cube it also moves forward as well. But it’s really move backwards in Lateral movement. But I am have issues solving on how to move this cube left and right with the 8 numpad key just like Blockland. And the rest of the numpad keys of 8,4,6 and 2.

public class MoveCubeFPSMovement : MonoBehaviour 
{
	[SerializeField] public float FPSCubeMovement = 1.0f;
	[SerializeField] public Transform target;
	[SerializeField] public float angle1 = 0f;

	void Update () 
	{
		Vector3 targetDir = target.position - transform.position;
		float angle1 = Vector3.Angle (targetDir, transform.forward);
		if (Input.GetKeyDown (KeyCode.Keypad8)) 
		{
			if (angle1 < 90.0f ) 
			{
				Debug.Log ("FPS cube moved backward");
				transform.position -= transform.forward * FPSCubeMovement;
			}
			if (angle1 > 90.0f ) 
			{
				Debug.Log ("FPS cube moved forward");
				transform.position += transform.forward * FPSCubeMovement;
			}
		}
	}
}

Your explanation is cristal clear. Now I understand much better the concept of extending a class. Thank you very much @rysan007

–