unity 相机平滑跟随为什么相机跟随脚本运行之后相机Z 轴是倒着的

摄像机跟随脚本 | Unity3D教程手册
当前位置 :
>> 摄像机跟随脚本
摄像机跟随脚本
distanceAway:摄像机和目标点的水平面距离。
distanceUp:摄像机和目标的高度差。
smooth:摄像机移动到目标点的平滑度,也可以理解为速度。
transform.LookAt(); :使摄像机始终朝向目标点。
脚本如下:
&&&01using UnityEngine;
02using System.Collections;
03&04public class ThirdPersonCamera : MonoBehaviour
public float distanceAway;
// distance from the back of the craft
public float distanceUp;
// distance above the craft
public float smooth;
// how smooth the camera movement is
private GameObject hovercraft;
// to store the hovercraft
private Vector3 targetPosition;
// the position the camera is trying to be in
Transform follow;
void Start(){
follow = GameObject.FindWithTag (&Player&).transform;
void LateUpdate ()
// setting the target position to be the correct offset from the hovercraft
targetPosition = follow.position + Vector3.up * distanceUp + follow.forward * distanceAway;
// making a smooth transition between it's current position and the position it wants to be in
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
// make sure the camera is looking the right way!
transform.LookAt(follow);
【上一篇】
【下一篇】
您可能还会对这些文章感兴趣!  3DTEXT 动态修改 字体。  text 动态修改文字。。  using S  using UnityE  using UnityEngine.UI;  using UnityStandardAssets.CrossPlatformI  namespace UnityStandardAssets.Characters.FirstPerson  {  [Serializable]  public class MouseLook  {  public float XSensitivity = 2f;  public float YSensitivity = 2f;  public bool clampVerticalRotation =  public float MinimumX = -90F;  public float MaximumX = 90F;    public float smoothTime = 5f;  public bool lockCursor =  private Quaternion m_CharacterTargetR  private Quaternion m_CameraTargetR  private bool m_cursorIsLocked =  public void Init(Transform character, Transform camera)  {  m_CharacterTargetRot = character.localR  m_CameraTargetRot = camera.localR  }  public void LookRotation(Transform character, Transform camera)  {
  float yRot = CrossPlatformInputManager.GetAxis("Mouse X")* YS  float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * XS  m_CharacterTargetRot *= Quaternion.Euler (0f,yRot, 0f);  m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);  if(clampVerticalRotation)  m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);  if(smooth)  {  character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,  smoothTime * Time.deltaTime);  camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,  smoothTime * Time.deltaTime);  }  else  {GameObject.Find("gtz").GetComponent&TextMesh&().text=xRot.ToString();  //上面这句是3D if ( Input.mousePosition.y& 100 ){character.localRotation = m_CharacterTargetR}  // character.localRotation = m_CharacterTargetR  camera.localRotation = m_CameraTargetR  //GameObject.Find("Canvas/ghl").Getcomponent().text="";
  GameObject.Find("Canvas/ghl").GetComponent&Text&().text= m_CameraTargetRot.ToString();  //上面这句是TEXTgo.GameObject.Find("gtz").GetComponent&TextMesh&().text="00000";  }  UpdateCursorLock();  }  public void SetCursorLock(bool value)  {  lockCursor =  if(!lockCursor)  {//we force unlock the cursor if the user disable the cursor locking helper  Cursor.lockState = CursorLockMode.N  Cursor.visible =  }  }  public void UpdateCursorLock()  {  //if the user set "lockCursor" we check & properly lock the cursos  if (lockCursor)  InternalLockUpdate();  }  private void InternalLockUpdate()  {  if(Input.GetKeyUp(KeyCode.Escape))  {  m_cursorIsLocked =  }  else if(Input.GetMouseButtonUp(0))  {  m_cursorIsLocked =  }  if (m_cursorIsLocked)  {  Cursor.lockState = CursorLockMode.L  Cursor.visible =  }  else if (!m_cursorIsLocked)  {  Cursor.lockState = CursorLockMode.N  Cursor.visible =  }  }  Quaternion ClampRotationAroundXAxis(Quaternion q)  {  q.x /= q.w;  q.y /= q.w;  q.z /= q.w;  q.w = 1.0f;  float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);  angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);  q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);    }  }  }
楼主发言:8次 发图:0张 | 更多
  if (Input.mousePosition.x &= Screen.width && Input.mousePosition.x &= Screen.width - 60) {  cameraFather.transform.Translate (new Vector3(1,0,0)*cameraSpeed*Time .deltaTime);  }else if(Input.mousePosition.x &= 0 && Input.mousePosition.x &= 60){  cameraFather.transform.Translate (new Vector3(1,0,0)*-cameraSpeed*Time .deltaTime);  }  if(Input.mousePosition.y &= 0 && Input.mousePosition.y &= 60){  cameraFather.transform.Translate (new Vector3(0,0,1)*-cameraSpeed*Time .deltaTime);  }else if(Input.mousePosition.y &= Screen.height-60 && Input.mousePosition.y &= Screen.height){  cameraFather.transform.Translate (new Vector3(0,0,1)*cameraSpeed*Time .deltaTime);  }  }  取屏幕的宽度。。高度。。如何去项目窗口的高度和宽度???
  鼠标往屏幕左边。。右边 ,上边
下边 。移动第一视角没有问题了。  using S  using UnityE  using UnityEngine.UI;  using UnityStandardAssets.CrossPlatformI  namespace UnityStandardAssets.Characters.FirstPerson  {  [Serializable]  public class MouseLook  {  public float XSensitivity = 2f;  public float YSensitivity = 2f;  public bool clampVerticalRotation =  public float MinimumX = -90F;  public float MaximumX = 90F;    public float smoothTime = 5f;  public bool lockCursor =  private Quaternion m_CharacterTargetR  private Quaternion m_CameraTargetR  private bool m_cursorIsLocked =  public void Init(Transform character, Transform camera)  {  m_CharacterTargetRot = character.localR  m_CameraTargetRot = camera.localR  }  public void LookRotation(Transform character, Transform camera)  {
  float yRot = CrossPlatformInputManager.GetAxis("Mouse X")* YS  float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * XS  //
m_CharacterTargetRot *= Quaternion.Euler (0f,yRot, 0f);  //
m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);  GameObject.Find("gtz").GetComponent&TextMesh&().text= Input.mousePosition.x.ToString()+ Screen.width.ToString();  if(clampVerticalRotation)  m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);  if(smooth)  {  character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,  smoothTime * Time.deltaTime);  camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,  smoothTime * Time.deltaTime);  }  else  if ( Input.mousePosition.y&=0&&xRot&0)
//上下动--往下动  {m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);  camera.localRotation = m_CameraTargetR  GameObject.Find("Canvas/ghl").GetComponent&Text&().text= m_CameraTargetRot.ToString();  }  if ( Input.mousePosition.y&= Screen.height-5&&xRot&0)//往上动  {  m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);  camera.localRotation = m_CameraTargetR  GameObject.Find("Canvas/ghl").GetComponent&Text&().text= m_CameraTargetRot.ToString();  }  if ( Input.mousePosition.x&=0&&yRot&0)
//--往左动  {m_CharacterTargetRot *= Quaternion.Euler (0f,yRot, 0f);  character.localRotation = m_CharacterTargetR  GameObject.Find("Canvas/ghl").GetComponent&Text&().text= m_CameraTargetRot.ToString();  }  if ( Input.mousePosition.x&=Screen.width -5 &&yRot&0)
//--往右动  {  m_CharacterTargetRot *= Quaternion.Euler (0f,yRot, 0f);  character.localRotation = m_CharacterTargetR  GameObject.Find("Canvas/ghl").GetComponent&Text&().text= yRot.ToString();  }  {//GameObject.Find("gtz").GetComponent&TextMesh&().text=xRot.ToString();  //if ( Input.mousePosition.y& 100 ){character.localRotation = m_CharacterTargetR}  // character.localRotation = m_CharacterTargetR  // camera.localRotation = m_CameraTargetR  //GameObject.Find("Canvas/ghl").Getcomponent().text="";
  //GameObject.Find("Canvas/ghl").GetComponent&Text&().text= Input.mousePosition.y.ToString();  //go.GameObject.Find("gtz").GetComponent&TextMesh&().text="00000";  }  UpdateCursorLock();  }  public void SetCursorLock(bool value)  {  lockCursor =  if(!lockCursor)  {//we force unlock the cursor if the user disable the cursor locking helper  Cursor.lockState = CursorLockMode.N  Cursor.visible =  }  }  public void UpdateCursorLock()  {  //if the user set "lockCursor" we check & properly lock the cursos  if (lockCursor)  InternalLockUpdate();  }  private void InternalLockUpdate()  {  if(Input.GetKeyUp(KeyCode.Escape))  {  m_cursorIsLocked =  }  else if(Input.GetMouseButtonUp(0))  {  m_cursorIsLocked =  }  if (m_cursorIsLocked)  {  Cursor.lockState = CursorLockMode.L  Cursor.visible =  }  else if (!m_cursorIsLocked)  {  Cursor.lockState = CursorLockMode.N  Cursor.visible =  }  }  Quaternion ClampRotationAroundXAxis(Quaternion q)  {  q.x /= q.w;  q.y /= q.w;  q.z /= q.w;  q.w = 1.0f;  float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);  angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);  q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);    }  }  }
  unity脚本自带函数执行顺序   在unity的脚本中,有大量的脚本执行按照预先确定的顺序执行的事件函数,由于篇幅有限,无法一一列举,这里只列举一些常用的自带函数的顺序,并论述他们之间的区别。  在Unity脚本中常用到的函数就是下面这些,他们的顺序也是按照箭头的方向执行的。  Awake -&OnEable-& Start -& FixedUpdate-& Update
-& LateUpdate -&OnGUI -&OnDisable -&OnDestroy  秉着实践是检验真理的唯一标准,咱们可以将下面脚本挂在一个物体运行,从打印结果中便可以看到执行顺序  void Awake()  {  print("Here is Awake");  }  void OnEnable()  {  print("Here is OnEnable");  }  void Start()  {  print("Here is Start");  }  void FixedUpdate()  {  print("Here is FixedUpdate");  }  void Update()  {  print("Here is Update");  }  void LateUpdate()  {  Debug.Log("Here is LateUpdate");  }  void OnGUI()  {  print("Here is OnGUI");  }  void Reset()  {  print("Here is OnReset");  }  void OnDisable()  {  print("Here is OnDisable");  }  void OnDestroy()  {  print("Here is OnDestroy");  }
下面是打印结果  接下来针对每个方法进行详细的说明  1.Awake  用于在游戏开始之前初始化变量或游戏状态。在脚本整个生命周期内它仅被调用一次.Awake在所有对象被初始化之后调用,所以你可以安全的与其他对象对话或用诸如GameObject.FindWithTag()这样的函数搜索它们。每个游戏物体上的Awake以随机的顺序被调用。因此,你应该用Awake来设置脚本间的引用,并用Start来传递信息Awake总是在Start之前被调用。它不能用来执行协同程序。  2.Start  仅在Update函数第一次被调用前调用。Start在behaviour的生命周期中只被调用一次。它和Awake的不同是Start只在脚本实例被启用时调用。你可以按需调整延迟初始化代码。Awake总是在Start之前执行。这允许你协调初始化顺序。在所有脚本实例中,Start函数总是在Awake函数之后调用。  3.FixedUpdate  固定帧更新,在Unity导航菜单栏中,点击“Edit”--&“Project Setting”--&“Time”菜单项后,右侧的Inspector视图将弹出时间管理器,其中“Fixed Timestep”选项用于设置FixedUpdate()的更新频率,更新频率默认为0.02s。  4.Update  正常帧更新,用于更新逻辑。每一帧都执行,处理Rigidbody时,需要用FixedUpdate代替Update。例如:给刚体加一个作用力时,你必须应用作用力在FixedUpdate里的固定帧,而不是Update中的帧。(两者帧长不同)FixedUpdate,每固定帧绘制时执行一次,和update不同的是FixedUpdate是渲染帧执行,如果你的渲染效率低下的时候FixedUpdate调用次数就会跟着下降。FixedUpdate比较适用于物理引擎的计算,因为是跟每帧渲染有关。Update就比较适合做控制。  Update()与FixedUpdate()的区别  从字面上理解,它们都是在更新时会被调用,并且会循环的调用。但是Update会在每次渲 染新的一帧时,被调用。而FixedUpdate会在每个固定的时间间隔被调用,那么要是Update 和FixedUpdate的时间间隔一样,是不是就一样呢?答案是不一定,因为Update受当前渲染的物体,更确切的说是三角形的数量影响,有时快有时 慢,帧率会变化,update被调用的时间间隔就发生变化。但是FixedUpdate则不受帧率的变化,它是以固定的时间间隔来被调用,那么这个时间间 隔怎么设置呢?Edit-&Project Setting-&time下面的Fixed timestep。  5.LateUpdate  在所有Update函数调用后被调用,和fixedupdate一样都是每一帧都被调用执行,这可用于调整脚本执行顺序。例如:当物体在Update里移动时,跟随物体的相机可以在LateUpdate里实现。LateUpdate,在每帧Update执行完毕调用,他是在所有update结束后才调用,比较适合用于命令脚本的执行。官网上例子是摄像机的跟随,都是在所有update操作完才跟进摄像机,不然就有可能出现摄像机已经推进了,但是视角里还未有角色的空帧出现。  Update和LateUpdate的区别  在圣典里LateUpdate被解释成一句话,LateUpdate是在所有Update函数调用后被调用。这可用于调整脚本执行顺序。例如:当物体在Update里移动时,跟随物体的相机可以在LateUpdate里实现。  LateUpdate是晚于所有Update执行的。例如:游戏中有2个脚本,脚本1含有Update和LateUpdate,脚本2含有Update,那么当游戏执行时,每一帧都是把2个脚本中的Update执行完后才执行LateUpdate 。虽然是在同一帧中执行的,但是Update会先执行,LateUpdate会晚执行。  现在假设有2个不同的脚本同时在Update中控制一个物体,那么当其中一个脚本改变物体方位、旋转或者其他参数时,另一个脚步也在改变这些东西,那么这个物体的方位、旋转就会出现一定的反复。如果还有个物体在Update中跟随这个物体移动、旋转的话,那跟随的物体就会出现抖动。 如果是在LateUpdate中跟随的话就会只跟随所有Update执行完后的最后位置、旋转,这样就防止了抖动。  6.OnGUI  在渲染和处理GUI事件时调用。比如:你画一个button或label时常常用到它。这意味着OnGUI也是每帧执行一次。  7.Reset  在用户点击检视面板的Reset按钮或者首次添加该组件时被调用。此函数只在编辑模式下被调用。Reset最常用于在检视面板中给定一个默认值。  8.OnDisable  当物体被销毁时 OnDisable将被调用,并且可用于任意清理代码。脚本被卸载时,OnDisable将被调用,OnEnable在脚本被载入后调用。注意: OnDisable不能用于协同程序。  9.OnDestroy  当MonoBehaviour将被销毁时,这个函数被调用。OnDestroy只会在预先已经被激活的游戏物体上被调用。注意:OnDestroy也不能用于协同程序。
  unity Transform类 Transform 变换,是场景中最常打交道的类,用于控制物体的位移,旋转,缩放等功能。   Transform  Class, inherits from Component, IEnumerable  Position, rotation and scale of an object.  控制物体的位置,旋转和缩放。  Every object in a scene has a Transform. It's used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierachically. This is the hierarchy seen in the Hierarchy pane. They also support enumerators so you can loop through children using:  //  Movesall transform children 10 units upwards!  for (var child: Transform  in transform) {  child.position +=   Vector3.up *10.0;  }  每一个场景中的物体都有Transform这个类,他是用来储存和处理物体的位移,旋转和缩放的。每一个Transform都可以有一个父物体,这可以让你得到下游节点的位移旋转和缩放。这个层次结构关系可以从Hierarchy面板中看到。他还支持enumerators,以使用循环来遍寻所有的子节点。  Variables  position: Vector3
物体在世界坐标中的位置。 transform.position=Vector3(10,10,10)//把物体放到(x=10,y=10,z=10)的位置  localPosition: Vector3
相对位置,或自身位置,物体相对于父物体的位置。  eulerAngles: Vector3
轴向旋转角度,相对于世界坐标。单位为度(°)  localPosition: Vector3 相对轴向旋转角度,或自身的旋转角度,物体相对于父物体的旋转。一般使用和处理物体的旋转角度时都会使用到这个数值。  right: Vector3
物体自身的红色轴向(X轴)在世界坐标中所指向的位置。注意在maya中x轴指向物体的左边,而unity中的x轴则指向物体的右边。rigidbody.velotity=transform.right*moveS//向物体的右侧以moveSpeed的速度移动  up: Vector3
物体自身的绿色轴向(Y轴)在世界坐标中所指向的位置。  forward: Vector3
物体自身的蓝色轴向(Z轴)在世界坐标中所指向的位置。  rotation: Quaternion
以四元数来表达的物体自身的旋转。四元数可以快速的处理物体的旋转方向的计算,搭配Quaternion可以用来计算各种需要的旋转方案。具体应用方法参见Quaternion篇  localRotation: Quaternion 相对于父物体的用四元数来表达的旋转。  localScale: Vector3 物体相对于父物体的缩放  parent: Transform 物体的父物体。 如果没有则返回null。 如果改变父物体的位置,旋转和缩放,会同时影响并改变子物体的位置,旋转和缩放,但是保持相对位置,相对旋转和相对缩放。  worldToLocalMatrix: Matrix4x4 一个表达从世界坐标到相对坐标位置的四维矩阵,Read Only。 如果你对矩阵不是很了解,请使用Transform.InverseTransformPoint。  LocalToWorldMatrix: Matrix4x4 一个表达从相对坐标到世界坐标位置的四维矩阵,Read Only。 如果你对矩阵不是很了解,请使用Transform.TransformPoint。  root: Transform
返回物体的最高层的父物体。如果物体本身就是最高层,则返回物体本身。  childCount: Int
返回物体的子物体数量。  lossyScale:Vector3 返回物体相对于世界坐标的缩放值。 Read Only。没什么用的一个属性,当子物体被旋转后也不是特别精确,也不推荐大家用。如果想要计算物体的世界坐标缩放,最好自己写计算公式。  Functions  1)Translate, 用来移动物体的函数,非常常用的一个函数。  function Translate (translation : Vector3, relativeTo : Space = Space.Self) : void  把物体向translation方向移动,距离为translation.magnitude。 relativeTo表示这个移动的参考坐标系。  function Translate (x : float, y : float, z : float, relativeTo : Space = Space.Self) : void  同上  function Translate (translation : Vector3, relativeTo : Transform) : void  如果relativeTo 不是null, 则以目标物体relativeTo的自身轴向作为参考坐标系。  function Translate (x : float, y : float, z : float, relativeTo : Transform) : void  同上  脚本:  var speed=30;  //将物体以30米每秒的速度向前移动。  trasform.Translate(Vector3.forward*speed*Time.deltaTime);  2)Rotate,用来旋转物体的函数,非常常用,在知道需要旋转的角度的情况下。如果要让物体旋转到指定位置,需要搭配Quaternion来使用。  function Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self) : void  旋转eulerAngles度(3个轴向分别旋转),以relativeTo为参考坐标系  function Rotate (xAngle : float, yAngle : float, zAngle : float, relativeTo : Space = Space.Self) : void  同上  function Rotate (axis : Vector3, angle : float, relativeTo : Space = Space.Self) : void  以axis为轴旋转angle度,以relativeTo为参考坐标系  3)RotateAround 让物体以某一点为轴心成圆周运动。  function RotateAround (point : Vector3, axis : Vector3, angle : float) : void  让物体以point为中心,绕axis为轴向旋转angle度。 保持原来与point的距离。  4)LookAt 让物体的z轴看向目标物体  function LookAt (target : Transform, worldUp : Vector3 = Vector3.up) : void  让物体的z轴看向target的位置,并以worldUp为y轴指向方向。  function LookAt (worldPosition : Vector3, worldUp : Vector3 = Vector3.up) : void  让物体看向worldPosition  5)TransformDirection  function TransformDirection (direction : Vector3) : Vector3  返回以物体自身为坐标轴的向量direction在世界坐标中的朝向向量。  function TransformDirection (x : float, y : float, z : float) : Vector3  同上  6)InverseTransformDirection  function InverseTransformDirection (direction : Vector3) : Vector3  function InverseTransformDirection (x : float, y : float, z : float) : Vector3  与TransformDirection相反,从世界坐标转换到自身相对坐标。  7)TransformPoint  function TransformPoint (position : Vector3) : Vector3  function TransformPoint (x : float, y : float, z : float) : Vector3  把一个点从自身相对坐标转换到世界坐标  8)InverseTransformPoint  function InverseTransformPoint (position : Vector3) : Vector3  function InverseTransformPoint (x : float, y : float, z : float) : Vector3  把一个点从时间坐标转换到自身坐标的位置。  9)DetachChildren  function DetachChildren () : void  把自身所有的子物体的父物体都设成世界,也就是跟自己的所有子物体接触父子关系。  10)Find  function Find (name : string) : Transform  找到一个名字是name的物体并返回  如果没有找到则返回null。 如果字符串被/隔离,函数则会像文件路径一样逐级下查。  //  Themagical rotating finger  function   Update() {  aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");  aFinger.Rotate(Time.deltaTime*20,0, 0);  }  11)IsChildOf  function IsChildOf (parent : Transform) : bool  如果物体是parent的父子层级关系下的一员,返回
  unity中鼠标经过一个物体时出现提示  首先被检测的物体要有collider  using UnityE  using System.C  public class Cube : MonoBehaviour {  //
public T  bool isShowT  //
// Use this for initialization  void Start () {  isShowTip=  }
  void OnMouseEnter () {  isShowTip=  //Debug.Log (cube.name);//可以得到物体的名字  }  void OnMouseExit () {  isShowTip=  }  void OnGUI () {  if (isShowTip){  GUI.Label(new Rect(Input.mousePosition.x,Screen.height-Input.mousePosition.y,100,40),"afdasdfasdf");   }
  }  }
  Unity组件:Rigidbody 刚体  Rigidbodies enable your GameObjects to act under the control of physics. The Rigidbody can receive forces and torque to make your objects move in a realistic way. Any GameObject must contain a Rigidbody to be influenced by gravity, act under added forces via scripting, or interact with other objects through the NVIDIA PhysX physics engine.  刚体使物体能在物理控制下运动。刚体可通过接受力与扭矩,使物体像现实方式一样运动。任何物体想要受重力影响,受脚本施加的力的作用,或通过NVIDIA PhysX物理引擎来与其他物体交互,都必须包含一个刚体组件。  Rigidbodies allow GameObjects to act under physical influence  刚体让物体在物理影响下运动。  Properties 属性  Mass 质量  The mass of the object (arbitrary units). It is recommended to make masses not more or less than 100 times that of other Rigidbodies.  物体的质量(任意单位)。建议一个物体的质量不要多于或少于其他单位的100倍。  Drag 阻力  How much air resistance affects the object when moving from forces. 0 means no air resistance, and infinity makes the object stop moving immediately.  当受力移动时物体受到的空气阻力。0表示没有空气阻力,极大时使物体立即停止运动。  Angular Drag 角阻力  How much air resistance affects the object when rotating from torque. 0 means no air resistance, and infinity makes the object stop rotating immediately.  当受扭力旋转时物体受到的空气阻力。0表示没有空气阻力,极大时使物体立即停止旋转。  Use Gravity 使用重力  If enabled, the object is affected by gravity.  若激活,则物体受重力影响。  Is Kinematic 是否是运动学  If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached.  若激活,该物体不再受物理引擎驱动,而只能通过变换来操作。适用于模拟运动的平台或者模拟受铰链关节连接的刚体。  Interpolate 插值  Try one of the options only if you are seeing jerkiness in your Rigidbody's movement.  当你发现刚体运动时抖动,可以尝试下面的选项。  None 无  No Interpolation is applied. 不应用插值。  Interpolate 内插值  Transform is smoothed based on the Transform of the previous frame.  基于上一帧的变换来平滑本帧变换。  Extrapolate 外插值  Transform is smoothed based on the estimated Transform of the next frame.  基于下一帧的预估变换来平滑本帧变换。  Freeze Rotation 冻结旋转  If enabled, this GameObject will never rotate based on collisions or forces added via script -- it will only rotate when using transform.Rotate().  若激活,物体将不会受碰撞或脚本施加的力而旋转——只能调用transform.Rotate()来旋转。  Collision Detection 碰撞检测  Used to prevent fast moving objects from passing through other objects without detecting collisions.  碰撞检测模式。用于避免高速物体穿过其他物体,却未触发碰撞。  Discrete 不连续  Use Discreet collision detection against all other colliders in the scene. Other colliders will use Discreet collision detection when testing for collision against it. Used for normal collisions (This is the default value).  不连续碰撞检测。使用不连续碰撞检测模式来与场景中其他碰撞器进行碰撞检测。其他物体与它的碰撞检测,也会应用这种模式。适用于普通碰撞(这是默认的模式)。  Continuous 连续  Use Discrete collision detection against dynamic colliders (with a rigidbody) and continuous collision detection against static MeshColliders (without a rigidbody). Rigidbodies set to Continuous Dynamic will use continuous collision detection when testing for collision against this rigidbody. Other rigidbodies will use Discreet Collision detection. Used for objects which the Continuous Dynamic detection needs to collide with. (This has a big impact on physics performance, leave it set to Discrete, if you don't have issues with collisions of fast objects)  连续碰撞检测。使用不连续碰撞检测来检测与动态碰撞器(刚体)的碰撞,使用连续碰撞检测来检测与静态网格(非刚体)的碰撞检测。采用连续动态碰撞检测模式的刚体碰见这类物体也将采用连续碰撞检测模式。而与其他刚体将采用不连续碰撞检测模式。这种模式适用于那些采用动态连续碰撞模式的物体碰撞的物体。(这对物理表现有很大的影响,如果你不关心与高速物体的碰撞,那么就让其为默认的不连续模式。)  Continuous Dynamic   动态连续  Use continuous collision detection against objects set to Continuous and Continuous Dynamic Collision. It will also use continuous collision detection against static MeshColliders (without a rigidbody). For all other colliders it uses discreet collision detection. Used for fast moving objects.  连续动态碰撞检测。使用连续动态碰撞检测模式来检测与连续模式和连续动态模式的物体间的碰撞。也适用于与静态网格(非刚体)的碰撞检测。而与之碰撞的其他模式的物体,采用的是不连续动态碰撞检测模式。适用于高速物体。  Constraints 约束  Restrictions on the Rigidbody's motion:-  对刚体运动的约束。  Freeze Position 冻结位置  Stops the Rigidbody moving in the world X, Y and Z axes selectively.  刚体在世界中沿所选X,Y,Z轴的移动,将无效。  Freeze Rotation 冻结旋转  Stops the Rigidbody rotating around the world X, Y and Z axes selectively.  刚体在世界中沿所选的X,Y,Z轴的旋转,将无效。  Details 细节  Rigidbodies allow your GameObjects to act under control of the physics engine. This opens the gateway to realistic collisions, varied types of joints, and other very cool behaviors. Manipulating your GameObjects by adding forces to a Rigidbody creates a very different feel and look than adjusting the Transform Component directly. Generally, you shouldn't manipulate the Rigidbody and the Transform of the same GameObject - only one or the other.  刚体让物体在物理引擎控制下运动。它可以通过真实碰撞来开门,实现各种类型的关节及其他很酷的行为。通过力来操纵物体,与直接通过变换不同,有一种不同的感觉。通常情况下,对同一物体,要么通过刚体操纵,要么通过变换操纵。  The biggest difference between manipulating the Transform versus the Rigidbody is the use of forces. Rigidbodies can receive forces and torque, but Transforms cannot. Transforms can be translated and rotated, but this is not the same as using physics. You'll notice the distinct difference when you try it for yourself. Adding forces/torque to the Rigidbody will actually change the object's position and rotation of the Transform component. This is why you should only be using one or the other. Changing the Transform while using physics could cause problems with collisions and other calculations.
  由于项目需要,要求用unity来展示三维场景,并在三维中能够方便的查询数据库等。一开始尝试在unity中直接连接数据库,当时连的xml,然而每次发布成网页后都会出现路径找不到等问题,所以迫不得已采用了unity向网页传送数据,网页中处理数据(查询数据库),然后将处理过的数据再反传送给unity,最终在unity中将其展示(在网页中展示更为灵活)。  原理很简单:  1、unity向网页发送数据的函数:Application.ExternalCall("SayHello",gameObject.name),这个函数将调用网页中的SayHello函数,gameObject.name为传递的参数。  2、网页向unity发送数据的函数:网页中用GetUnity().SendMessage(message, "AcceptName", buildingname)函数来调用unity中的函数,此函数的参数message为unity中的物体,AcceptName为物体上的函数,buildingname为传递的参数。  网页中的函数如下:  [csharp] view plain copy  function SayHello(message){//此函数来接收unity中发送出来的message值,并将处理后的数据再发送回unity中
  jQuery.post('../Unity/javascript/DBhelper.ashx', {id:message}, function(data)
  var msg=JSON.parse(data);//将json数据解析
  var buildingname = msg[0].Building_
  var buildingcategory=msg[0].Building_
  var buildingpic = msg[0].Building_
  GetUnity().SendMessage(message, "AcceptName", buildingname);//向unity中的message物体上的MyFunction函数发送buildingname值
  GetUnity().SendMessage(message, "AcceptCategory", buildingcategory);
  GetUnity().SendMessage(message, "AcceptImg", buildingpic);
  此函数将unity中发送的数据message传到DBhelper.ashx中,在DBhelper.ashx中将传递过来的数据进行查询等操作,然后再用GetUnity().SendMessage(message, "AcceptName", buildingname)将处理好的数据buildingname传给unity中的AcceptName函数。  以下是unity中的脚本,可以实现中文,关于中文的实现由于文章有限,在此不再说明,只说明怎样接收网页中的数据。  [javascript] view plain copy  var chineseSkin : GUIS//在此可以选择字体,并设置为中文。建议编辑器设为uft-8。
  var buildingname:S//用来接收从网页中传递过来的buildingname值
  var buildingcategory:S//用来接收从网页中传递过来的buildingcategory值
  var buildingpic:Texture2D;//用来接收从网页中传递过来的buildingpic值
  var windowRect0 = Rect (20, 20, 250, 200);
  var enable:
  function Awake(){
  enable =
  function OnMouseDown () {
  Application.ExternalCall("SayHello",gameObject.name);// 向网页中的SayHello函数发送gameObject.name数据
  enable =
  function AcceptName(bdname){//用于接收网页中发送回来的数据
  buildingname=
  function AcceptCategory(buildingType){//用于接收网页中发送回来的数据
  buildingcategory=buildingT
  function AcceptImg(img){
  var www :WWW = new WWW("http://localhost:1166/Unity/images/"+img+"");
  buildingpic=www.
  function OnGUI(){
  GUI.skin=chineseS
  if(enable)
  windowRect0 = GUI.Window (0, windowRect0, DoMyWindow, "属性");
  function DoMyWindow (windowID : int) {
  GUI.Label(Rect(10,50,80,30),"建筑物名字");
  GUI.TextField(Rect(100,50,100,30),buildingname);
  GUI.Label(Rect(10,100,80,30),"建筑物类型");
  GUI.TextField(Rect(100,100,100,30),buildingcategory);
  GUI.DrawTexture(Rect(10,150,200,50),buildingpic,ScaleMode.ScaleToFit,true,0);
  if(GUI.Button(Rect(190,20,50,30),"退出")){
  enable =
  GUI.DragWindow (Rect (0,0,));
  function OnMouseOver(){
  transform.Rotate(0,Time.deltaTime*100,0,Space.World);
  function OnMouseEnter(){
  renderer.material.color = Color.
  function OnMouseExit(){
  renderer.material.color = Color.
  这是unity中的脚本,此脚本实现点击物体,弹出物体的属性。  引自:/Mygirl/archive//2003116.html
<span class="count" title="万
请遵守言论规则,不得违反国家法律法规回复(Ctrl+Enter)}

我要回帖

更多关于 unity 相机跟随物体 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信