[Basic operations for U3D Crane, script binding, and control its movement]

Source: Internet
Author: User

I was learning unity3d before. I don't know why there are really few teaching resources on the Internet... I am still stuck at some time and don't know how to make the object bind a script for automatic motion ..

The reason why I want to learn about U3D is that the mentor asked me to simulate a crane in my IOS project to control the animation of a real crane (why not an excavator )...

Here we will summarize the basic knowledge of U3D.

========================================================== ============

1. Unity draws objects

In the hierarchy table on the left, locate create and select cube. You can also select gameobject creation from the menu bar above.

Cubes are cubes.

After the creation is complete, click the object and set the length, width, height, XYZ axis, width, and so on in the inspector on the right side. You can build a crane on your own. How can this happen depends on talent ..

 

This is almost the case. Adjust the light source and camera angle.

 

2. Bind a script to the object.

Right-click the assets folder below to create a JS script and copy the following.

// 0 = stop, 1 = forward, 2 = backvar move = 0; // draw the UI interface function ongui () {// set the GUI background color GUI. backgroundcolor = color. red; If (GUI. button (rect (,), "Move Forward") {move = 1; // move forward if (transform. position. x <10) {transform. position. X ++ = 0.1;} */} If (GUI. button (rect (10, 50, 70, 30), "move backward") {move = 2; // move backward if (transform. position. x>-9.5) {transform. position. x-= 0.1;} * // displays the model location information GUI. label (rect (,), "Model location" + transform. position);} function setxposition (message: string) {var xposition = parsefloat (Message); transform. position. X = xposition;} function start () {setxposition ("-5");} function Update () {Switch (MOVE) {Case 0: break; Case 1: if (transform. position. x <10) {transform. translate (time. deltatime * 1, 0, 0);} break; Case 2: If (transform. position. x>-9.5) {transform. translate (time. deltatime *-1, 0, 0);} break; default: break ;}}

Bound to the object to be bound. If you want to bind multiple objects, such as the middle pole, rope (simulated by cube), and the following heavy objects, let them move forward together, create an empty object for the gameobject, and drag the three into and combine them into one and then set them.

Bind the Script: drag the script directly to the inspector of the object.

3. Study the script:

In the code, the ongui () function creates two buttons for user control. The update () function refreshes every frame. In this case, you can click the button to set its motion mode. The next excavator will go beyond the direction you set.

But there is no stop button here, so it cannot be stopped at all...

Of course I set some borders to prevent the crane from dismounting... --

 

4. Get the gameobject (get the hook and rope and let them rise and fall)

// 0 = stop, 1 = up, 2 = downvar move = 0; function ongui () {// set the GUI background color GUI. backgroundcolor = color. red; If (GUI. repeatbutton (rect (10,190,), "Drop hook") {move = 1; // drop hook shengzi. localscale. Y ++ = 0.05; shengzi. position. y-= 0.025; gouzi. position. y-= 0.05; */} If (GUI. repeatbutton (rect (10,230,), "Raised hook") {move = 2; // raised hook/* If (gouzi. position. Y <19) {shengzi. localscale. y-= 0.05; shengzi. position. Y ++ = 0.025; gouzi. position. Y + = 0.05;} */} function moveup () {move = 1;} function movedown () {move = 2;} function setyposition (message: string) {var yheight = parsefloat (Message); var shengzi = transform. find ("shengsuo"); var gouzi = transform. find ("gouzi"); shengzi. localscale. y = (20-yheight); shengzi. position. y = 20-(20-yheight)/2; gouzi. position. y = yheight;} function start () {setyposition ("15");} function Update () {var shengzi = transform. find ("shengsuo"); var gouzi = transform. find ("gouzi"); Switch (MOVE) {Case 0: break; Case 1: If (gouzi. position. y> 0) {shengzi. localscale. Y ++ = 0.05; shengzi. position. y-= 0.025; gouzi. position. y-= 0.05;} break; Case 2: If (gouzi. position. Y <19) {shengzi. localscale. y-= 0.05; shengzi. position. Y ++ = 0.025; gouzi. position. Y + = 0.05;} break; default: break ;}}

First, the correct part of this Code is the method for obtaining objects, which is hard to find on the Internet:

VaR shengzi = transform. Find ("shengsuo ");
"Shengsuo" is the name of the created gameobject.

I don't know why I want to use these messy numbers because I don't know how to set a certain segment of the rope as the center point. Once the length is changed, the stretch ends at both ends, so in order to simulate the descent, change the center of gravity down .. Anyone who knows this can teach me in the comment area. 3q.

 

Final effect:

[Basic operations for U3D Crane, script binding, and control its movement]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.