您的位置:首页 > 移动开发 > Unity3D

Unity Reading Notes

2016-07-12 19:31 411 查看
2016-7-12 18:59:39

add   force 

x  100  z 0   y   100

GameObject addforceObj=null;//add force to this obj

GameObject addposObj=null;// target position of force

GameObject cubeObj=null;//

 //in  start function   assign values to a propertys.

addPosObj=GameObject.find("obj1");

addPosObj= cubeObj=

//normal force

addForceObj.rigidbody.AddForce(100,0,100);//xzy  z:0

//location force

Vector3 force =cubeObj.transform.position-addposObj.transform.position;

addposObj.rigidbody.AddForceATPosition(force.addposObj.transform.position,ForceMode.impulse);

Rigidbody.AddForceAtPosition

public void AddForceAtPosition(Vector3 force, Vector3 position, ForceMode mode = ForceMode.Force);

using UnityEngine;

using System.Collections;

public class ExampleClass : MonoBehaviour {

    void ApplyForce(Rigidbody body) {

        Vector3 direction = body.transform.position - transform.position;

        body.AddForceAtPosition(direction.normalized, transform.position);

    }

}

-----2016-7-12 19:12:06

unity5.X cook  book

p147

In Unity 2D animations can be created in several different ways – one way is to create many

images, each slightly different, which frame-by-frame give the appearance of movement.

Both sources of animations become Animation Clips in the Animation panel. Each Animation

Clip then becomes a State in the Animator Controller State Machine.

To flip an object horizontally with arrow key presses, follow these steps:

1. Create a new Unity 2D project.

2. Import the provided image  EnemyBug.png .

3. Drag an instance of the red Enemy Bug image from the Project | Sprites folder

into the scene. Position this GameObject at ( 0 ,  0 ,  0 ) and scale to ( 2 ,  2 ,  2 ).

4. Add an instance of C# script class  BugFlip as a component to your Enemy

Bug GameObject:

using UnityEngine;

using System.Collections;

public class BugFlip : MonoBehaviour {

private bool facingRight = true;

void Update() {

if (Input.GetKeyDown(KeyCode.LeftArrow) && facingRight)

Flip ();

if (Input.GetKeyDown(KeyCode.RightArrow) && !facingRight)

Flip();

}

void Flip (){

// Switch the way the player is labelled as facing.

facingRight = !facingRight;

// Multiply the player's x local scale by -1.

Vector3 theScale = transform.localScale;

theScale.x *= -1;

transform.localScale = theScale;

}

}

5. When you run your scene, pressing the Left and Right arrow keys should make the

bug face left or right correspondingly.

-------------------how it work------------------

The C# class defines a Boolean variable  facingRight , which stores a  true / false value

corresponding to whether or not the bug is facing right or not.Since our bug sprite is initially

facing right, then we set the initial value of  facingRight to true to match this.

Method  Update() , every frame, checks to see if the Left or Right arrow keys have been

pressed. If the Left arrow key is pressed and the bug is facing right, then method  Flip() is

called, likewise if the Right arrow key is pressed and the bug is facing left (that is, facing right

is false), again method  Flip() is called.

Method  Flip() performs two actions, the first simply reverses the true/false value in variable

facingRight . The second action changes the +/- sign of the X-value of the  localScale

property of the transform. Reversing the sign of the  localScale results in the 2D flip that

we desire. Look inside the  PlayerControl script for the BeanMan character in the next

recipe – you'll see exactly the same  Flip() method being used.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: