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.
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.
相关文章推荐
- 记一次基于Unity的Profiler性能分析
- Unity Profile相关
- untiy 3d ShaderLab_第6章_VertexLit渲染路径_4_顶点照明和Unity存放光源的第三种方式
- untiy3d网格合并和保存
- 在unity中打开ios手机上的其他app
- 一个剪切shader 液体水
- unity3D中协程和线程混合
- UNITY的drawcall计算
- 「Unity」UGUI的Text实现首行缩进的办法
- Unity 玩家设置
- Eclipse和Unity交互实现微信登录
- [UnityShader3]转圈与冷却效果
- 一个解决背景切换的shader
- Unity IPv6 Socket 支持
- Unity3d异步记载场景&制作Loading场景进度条
- 【Unity Shaders】法线纹理(Normal Mapping)的实现细节
- Unity3d-Vuforia-Android 开发环境搭建
- Unity3D MonoDevelop unsafe code may only appear if compiling with /unsafe
- 【Unity3D自学记录】Unity Standard Assets 简介之 Characters
- unity 控制旋转的方法