Blog icon indicating copy to clipboard operation
Blog copied to clipboard

Unity3D Notes

Open dabing1022 opened this issue 9 years ago • 1 comments

1.Look towards player
using UnityEngine;
using System.Collections;

public class LookTowardMouse : MonoBehaviour {

     void Update () 
     {
         //Mouse Position in the world. It's important to give it some distance from the camera. 
         //If the screen point is calculated right from the exact position of the camera, then it will
         //just return the exact same position as the camera, which is no good.
         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);

         //Angle between mouse and this object
         float angle = AngleBetweenPoints(transform.position, mouseWorldPosition);

         //Ta daa
         transform.rotation =  Quaternion.Euler (new Vector3(0f,0f,angle));
     }

     float AngleBetweenPoints(Vector2 a, Vector2 b) {
         return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
     }  
 }

dabing1022 avatar Oct 11 '15 10:10 dabing1022

  • 当RigidBody2D的质量属性被设置为0时,刚体的质量变为无限大,此时刚体相当于静态刚体,永远一动不动。但是在Unity中你是无法把一个RigidBody2D的质量设置为0的,所以,当你想创建一个静态刚体时,只需要创建碰撞器,而不需要创建RigidBody2D。
  • 当我们勾选了is Kenamatic选项时,这个RigidBody2D就不再受物理引擎控制,我们这时候需要通过Transform来控制RigidBody2D的移动,这个选项经常在脚本里对其进行设置。
  • 当Gravity Scale设置为0时,该刚体不再受重力影响。

dabing1022 avatar Oct 11 '15 10:10 dabing1022