Jan
14
2012

Walking Block Rotation

Testing rotating / walking block code.. (as seen in many puzzle games)

Current progress:
– Screenshot: Step.1 and Step.2 (going towards right direction)
– Can walk one direction..
– Need to add smooth movement (now it rotates 90 degrees instantly)

*This one failed.. not going to fix it
(if you want the demo, I can post.. but it only works in 2 directions)

Source (javascript)

public var pivotball:Transform[];

function Update () 
{

	if (Input.GetKeyDown("a")) // left
	{
		// function RotateAround (point : Vector3, axis : Vector3, angle : float) : void 
		
		var axis = Vector3(0,0,1);
		var n = 0;
		// which point is lowest, in that direction?
		for (var i:int=0;i<pivotball.length;i++)
		{
			if (pivotball[i].position.y<transform.position.y)
			{
				if (pivotball[i].position.x<transform.position.x)
				{
					n=i;
					break;
				}
			}
		}
		
		transform.RotateAround (pivotball[n].position,axis, 90);
	}

	if (Input.GetKeyDown("d")) // right
	{
		var n2=0;
		for (var i2:int=0;i2<pivotball.length;i2++)
		{
			if (pivotball[i2].position.y<transform.position.y)
			{
				if (pivotball[i2].position.x>transform.position.x)
				{
					n2=i2;
					break;
				}
			}
		}
	
		var axis2 = Vector3(0,0,1);
		transform.RotateAround (pivotball[n2].position,axis2, -90);
	}

	if (Input.GetKeyDown("w")) // up
	{
		var n3=0;
		for (var i3:int=0;i3<pivotball.length;i3++)
		{
			if (pivotball[i3].position.y<transform.position.y)
			{
				if (pivotball[i3].position.z>transform.position.z)
				{
					n3=i3;
					break;
				}
			}
		}
	
		var axis3 = Vector3(1,0,0);
		transform.RotateAround (pivotball[n3].position,axis3, 90);
	}

	if (Input.GetKeyDown("s")) // down
	{
		var n4=0;
		for (var i4:int=0;i4<pivotball.length;i4++)
		{
			if (pivotball[i4].position.y<transform.position.y)
			{
				if (pivotball[i4].position.z<transform.position.z)
				{
					n4=i4;
					break;
				}
			}
		}
	
		var axis4 = Vector3(1,0,0);
		transform.RotateAround (pivotball[n4].position,axis4, -90);
	}


}

Screenshot:
block_walk_ss
 

 


Related Posts

About the Author:

.fi

2 Comments + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.