public static VectorDirection ( JCS_Vector3Direction direction ) : Vector3 | ||
direction | JCS_Vector3Direction | direction u wants. |
return | Vector3 |
/// <summary> /// Source: http://wiki.unity3d.com/index.php?title=LookAtMouse /// </summary> private void LookAtMouse() { float speed = 10; Vector3 direction = JCS_Util.VectorDirection(mDirection); // Generate a plane that intersects the transform's position with an upwards normal. Plane playerPlane = new Plane(direction, mShootAction.SpawnPoint.position); // Generate a ray from the cursor position Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Determine the point where the cursor ray intersects the plane. // This will be the point that the object must look towards to be looking at the mouse. // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance, // then find the point along that ray that meets that distance. This will be the point // to look at. float hitdist = 0.0f; // If the ray is parallel to the plane, Raycast will return false. if (playerPlane.Raycast(ray, out hitdist)) { // Get the point along the ray that hits the calculated distance. Vector3 targetPoint = ray.GetPoint(hitdist); // Determine the target rotation. This is the rotation if the transform looks at the target point. Quaternion targetRotation = Quaternion.LookRotation(targetPoint - mShootAction.SpawnPoint.position); // Smoothly rotate towards the target point. mShootAction.SpawnPoint.rotation = Quaternion.Slerp(mShootAction.SpawnPoint.rotation, targetRotation, speed * Time.deltaTime); } }
/// <summary> /// Set on top of the box above the ray. /// </summary> /// <param name="cap"></param> /// <param name="maxDistance"></param> public static bool SetOnTopOfClosestBoxWithRay( CharacterController cap, float maxDistance, JCS_Vector3Direction inDirection) { Vector3 direction = cap.transform.TransformDirection(JCS_Util.VectorDirection(inDirection)); RaycastHit[] hits = Physics.RaycastAll(cap.transform.position, direction, maxDistance); /* Nothing detected. */ if (hits.Length == 0) { return(false); } foreach (RaycastHit hit in hits) { /* Check if there are box collider on hit. */ BoxCollider boxCollider = hit.transform.GetComponent <BoxCollider>(); if (boxCollider == null) { continue; } /* Check ray ignore attached. */ JCS_RayIgnore ri = hit.transform.GetComponent <JCS_RayIgnore>(); if (ri != null) { continue; } // set on top of that box SetOnTopOfBox(cap, boxCollider); return(true); } return(false); }
/// <summary> /// Do the look at algorithm here... /// </summary> private void DoLookAt() { // record down the euler angle before we changes. if (mLocalEulerAngles) { mLastEulerAngles = this.transform.localEulerAngles; } else { mLastEulerAngles = this.transform.eulerAngles; } Vector3 lookPoint = mTargetTransform.position; Vector3 direction = Vector3.up; // get direction according to the type. direction = JCS_Util.VectorDirection(mLookDirection); transform.LookAt(lookPoint, direction * (int)mState); // apply offset angle if (mLocalEulerAngles) { this.transform.localEulerAngles += mAngleOffset; } else { this.transform.eulerAngles += mAngleOffset; } // TODO(jenchieh): study the rotation going on in Unity lower // level archietecture. // // rotate back to X-axis. if (mRotateBack90) { transform.Rotate(0, -90, 0); } }