Nuts and bolts with blender and Unity 3d written in Java


Java-Script for animating an Bolt in Unity 3D
quaintproject.wordpress.com fall 2012

Hey guys,

Quaintproject comes up with a new java script. We developed that simple but very handy script during the process of further developing our Ball in Maze Game.

This script can be used quite randomly, you can use it for arrows which show you the finish. You could also use it for Bolts and Nuts like we did. This time you have the choice between watching our tutorial or copy and paste.  Give it a try!

Of course you are free to use this script (just as usual). If you redistribute it, it would be kind to mention us in your link section;)

If you have any questions just contact us on our Facebook page or leave a comment below this article!

Cheers

Video:

This is actually our first video showing the process of creating a script.

This is the  script mentioned above:

#pragma strict

//at first we define our variables

var ActualGameObj : GameObject;

private var startpoint : Vector3;

private var temppoint : Vector3;

private var plus : boolean = true;

var Movement : float;

var MaxMovement : float;

var RotationX : float = 0;

var RotationY : float = 0;

var RotationZ : float = 0;

//next Step is initializing our Vector's startpoint and temppoint (temppoint will hold the latest position 
//from function FixedUpdate()

function Start () {

startpoint = ActualGameObj.transform.position;

temppoint = startpoint;

}

 // in the FixedUpdate function we check the actual position of temppoint. If its position Y  is greater than// or equal to the  start point we set plus true. If it's smaller than or equals to the 
//start point +MaxMovement (the maximum movement of our screw) we set plus = false.
temppoint.y >= startpoint.y is true we'll set plus = true

function FixedUpdate () {

if(temppoint.y >= startpoint.y)
//if you want to animate an unscrewing bolt, you need to change the _
//if-statement to (temppoint.y <= startpoint.y) and the else if to 
//(temppoint.y >= startpoint.y + MaxMovement)

{

plus= true;

}

else if(temppoint.y <= startpoint.y + MaxMovement)

{

plus = false;

}

//The following if-else statement causes the actual rotation and movement.

if(plus == true)

{
//moves our gameObject along the y-axis for our variable Movement, which can be edited in Unity 3D
ActualGameObj.gameObject.transform.position.y = temppoint.y + Movement;
//This statement overwrites the variable temppoint with our actual position.
temppoint = ActualGameObj.transform.position;
//Now lets do the same for the rotation.
ActualGameObj.transform.Rotate(RotationX, RotationY, RotationZ);

}

else

{
//This statement moves our object along the y-axis but negatively!
ActualGameObj.gameObject.transform.position.y = temppoint.y - Movement;
//This statement overwrites the variable temppoint with our actual position.
temppoint = ActualGameObj.transform.position;
//Now lets do the same for the rotation. ( Notice, the Rotation must be negative!)
 ActualGameObj.transform.Rotate(-RotationX, -RotationY, -RotationZ);  }  }
//Scripting by Alexander Bilz/ Quaintproject 2012;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s