unity3d如何导入模型 把现成的场景导入打开 怎么不能用游戏视角 在场景里走动 想添加人物

【图文】Unity3D游戏场景设计实例教程ppt_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Unity3D游戏场景设计实例教程ppt
&&Unity3D游戏场景设计实例教程ppt
大小:31.73MB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢原创文章如需转载请注明:转载自
本文链接地址:
Hierarchy 层次视图:
层次视图(Hierarchy)包含了每一个当前场景的所有游戏对象(GameObject)。其中一些是资源文件的实例,如3D模型和其他预制物体(Prefab)的实例。您可以在层次结构视图中选择对象或者生成对象。当在场景中增加或者删除对象,层次结构视图中相应的对象则会出现或消失。
Unity使用父对象的概念。要想让一个游戏对象成为另一个的子对象,只需在层次视图中把它拖到另一个上即可。一个子对象将继承其父对象的移动和旋转属性。(在层级视图面板展开和折叠父对象来查看它的子对象,不会影响游戏。)
&Scene View 场景视图:
场景视图(Scene View)是交互式沙盒,可以使用它来选择和布置环境、玩家、摄像机、敌人和所有其他游戏对象。
在层次视图(Hierarchy)中选择某物体按下&F健(Frame Selected)&,可以在场景视图(Scene View)快速找到该物体。
&Project 工程视图:
每个Unity的项目包含一个资源文件夹。此文件夹的内容呈现在项目视图。这里存放着你游戏的所有资源,比如场景、脚本、三维模型、纹理、音频文件和预制组件。如果你在项目视图里右击任何资源,你都可以在资源管理器中(在Mac系统中是Reveal in Finder)找到这些真正的文件本身。
重要提示:不要使用操作系统来移动项目资源,因为这将破坏与资源相关的一些元数据。你应该始终使用项目视图来组织自己的资源。
要添加资源到你的项目中,你可以拖动操作系统的任何文件到项目视图,或者使用Assets & Import New Asset导入新资源。你的资源现在可以在你的游戏中使用了。
有些游戏资源必须从Unity内部建立。要做到这一点,使用Create下拉菜单,或通过右键然后点击 & Create来创建。这将允许你添加脚本、预制物体(Prefab)、或文件夹让您的项目组织有序。
你可以在Windows系统中按F2键(Mac系统中为回车)重新命名任何资源/文件夹,或通过在资源名字上两次点击(不是双击)来重命名。如果你按住Alt键的同时,展开或收起一个目录,所有子目录也将展开或收起。
&Inspector检视面板:
显示当前当前选定的游戏对象的所有附加组件(脚本属于组件)及其属性的相关详细信息。
锁定选项让你在选择其他游戏对象时,保持锁定的面板始终在检视面板中。&
Debug调试模式让你可以在检视视图中查看组件的私有变量,通常它们不会显示。(面板右上角下拉菜单选择)
Toolbar 工具栏:
&播放 | 暂停&|&步进
点击播放可立即运行游戏。
暂停用于分析复杂的行为,游戏过程中(或暂停时)可以修改参数、资源、甚至是脚本。
播放或暂停中修改的数据在停止后会还原到播放前的状态。
用于改变视图模式。
将个窗口或大小挑动至自己最舒服的状态,在右侧下拉中可选择&Save Layout&保存视图。
变换工具:用于场景视图改变左键功能,任何状态下滚轮为放大/缩小键,右键为旋转视角键。
手柄工具(快捷键Q):按住左键拖动视角。
移动工具(快捷键W):首先选择物体,物体会出现方向轴,拖动方向轴移动物体。
旋转工具(快捷键E):首先选择物体,物体会出现转转轴,拖动旋转轴旋转物体。
缩放工具(快捷键R):首先选择物体,物体会出现缩放方向轴,拖动可缩放物体大小。
&Game View游戏视图:
显示你最后发布游戏后的运行画面,你需要使用一个或多个摄像机来控制玩家在游戏时实际看到的画面,播放状态执行gameobject中所有的组件。
点击 播放 进入播放模式。
&控制条的第一个下拉菜单是Aspect外观菜单,在这里,你可以强制游戏窗口为不同的长宽比,它可以用来测试游戏在不同长宽比的显示器中的不同情况。
阅读(...) 评论()unity3d人物切换场景如何实现?
unity3d人物从一个场景传送到另一个场景,该怎么实现啊,请教各位大虾!!!!
09-09-08 &
炫舞里的道具你去玩道具模式,个人认为没什么意思。另外还有道具可以让人物变成企鹅之类的动物,你可以做任务获得道具。切换场景可以把场景取消掉,只剩几个人在画面上,缺少美感。我的家在你刚进入的时候左上角?右上角?记不得了,注意看下就可以了
请登录后再发表评论!程序写累了,就来玩玩酷跑小游戏吧,嘿嘿。
雨松MOMO送你一首歌曲,嘿嘿。
Unity3D研究院之处理摄像机跟随避免相机穿墙拉近或透明的方法(四十四)
Unity3D研究院之处理摄像机跟随避免相机穿墙拉近或透明的方法(四十四)
围观56517次
编辑日期: 字体:
当摄像机处于跟随主角状态时,那么主角移动后很有可能摄像机被别的模型挡住。这样用户体验就非常不好,一般3D游戏在处理视角的时候有两种方法,第一种是让被挡住的模型变成透明,第二种是拉近摄像机。前者时候固定视角游戏使用,后者适合变化视角游戏使用。两个我们都学一学蛤蛤。
如下图所示,MOMO写了一个简单的例子,通过鼠标或触摸来牵引主角向不同的角度移动,旁边有一面墙当主角被这面墙挡住,此时摄像机将拉近。
首先,为了方面鼠标与移动平台上的触摸同时相应我写了一个通用的方法。
包括 开始触摸
触摸中 结束触摸 ,电脑手机都可以用这个方法相应到。
JFConst.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445
using UnityEngine;using System.Collections;&public class JFConst{& public static&&bool TouchBegin() {
if(Input.GetMouseButtonDown(0))
return true;
if(Input.touchCount & 0 && Input.GetTouch(0).phase == TouchPhase.Began)
return true;
return false; }& public static bool TouchEnd() {
if(Input.GetMouseButtonUp(0))
return true;
if(Input.touchCount & 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
return true;
return false; }& public static bool TouchIng() {
if(Input.GetMouseButton(0))
return true;
}else if(Input.touchCount & 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
return true;
return false; }&}
以前写过一篇文章关于移动主角的,如果对移动主角还有不懂的请看这篇
。 这里我就不解释移动主角这部分的代码了。 下面是角色控制器ThirdPersonController.cs 绑定在主角身上。
下面代码中需要详细看的就是161行到174行 ,这部分就是根据触摸屏幕的2D坐标计算主角在3D世界中移动的坐标。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
using UnityEngine;using System.Collections;&/** *&&@Author :
*/&[RequireComponent(typeof(CharacterController))]&public class ThirdPersonController : MonoBehaviour {&public AnimationClip idleAnimation ;public AnimationClip walkAnimation ;public AnimationClip runAnimation ;public AnimationClip jumpPoseAnimation;&public float walkMaxAnimationSpeed&&= 0.75f;public float trotMaxAnimationSpeed&&= 1.0f;public float runMaxAnimationSpeed&&= 1.0f;public float jumpAnimationSpeed&&= 1.15f;public float landAnimationSpeed&&= 1.0f;&private Animation _animation;&enum CharacterState{ Idle = 0, Walking = 1, Trotting = 2, Running = 3, Jumping = 4,}&private CharacterState _characterState;&// The speed when walkingpublic float walkSpeed = 2.0f;// after trotAfterSeconds of walking we trot with trotSpeedpublic float trotSpeed = 4.0f;// when pressing "Fire3" button (cmd) we start runningpublic float runSpeed = 6.0f;&public float inAirControlAcceleration = 3.0f;&// How high do we jump when pressing jump and letting go immediatelypublic float jumpHeight = 0.5f;&// The gravity for the characterpublic float gravity = 20.0f;// The gravity in controlled descent modepublic float speedSmoothing = 10.0f;public float rotateSpeed = 500.0f;public float trotAfterSeconds = 3.0f;&public bool canJump = true;&private float jumpRepeatTime = 0.05f;private float jumpTimeout = 0.15f;private float groundedTimeout = 0.25f;&// The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around.private float lockCameraTimer = 0.0f;&// The current move direction in x-zprivate Vector3 moveDirection = Vector3.zero;// The current vertical speedprivate float verticalSpeed = 0.0f;// The current x-z move speedprivate float moveSpeed = 0.0f;&// The last collision flags returned from controller.Moveprivate CollisionFlags collisionFlags; &// Are we jumping? (Initiated with jump button and not grounded yet)private bool jumping = false;private bool jumpingReachedApex = false;&// Are we moving backwards (This locks the camera to not do a 180 degree spin)private bool movingBack = false;// Is the user pressing any keys?private bool isMoving = false;// When did the user start walking (Used for going into trot after a while)private float walkTimeStart = 0.0f;// Last time the jump button was clicked downprivate float lastJumpButtonTime = -10.0f;// Last time we performed a jumpprivate float lastJumpTime = -1.0f;&// the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)private float lastJumpStartHeight = 0.0f;&private Vector3 inAirVelocity = Vector3.zero;&private float lastGroundedTime = 0.0f;&private bool isControllable = true;&void Awake (){ moveDirection = transform.TransformDirection(Vector3.forward);& _animation = GetComponent&Animation&(); if(!_animation)
Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");& /*public var idleAnimation : AnimationCpublic var walkAnimation : AnimationCpublic var runAnimation : AnimationCpublic var jumpPoseAnimation : AnimationC */ if(!idleAnimation) {
_animation = null;
Debug.Log("No idle animation found. Turning off animations."); } if(!walkAnimation) {
_animation = null;
Debug.Log("No walk animation found. Turning off animations."); } if(!runAnimation) {
_animation = null;
Debug.Log("No run animation found. Turning off animations."); } if(!jumpPoseAnimation && canJump) {
_animation = null;
Debug.Log("No jump animation found and the character has canJump enabled. Turning off animations."); }&}&void UpdateSmoothedMovementDirection (){& Transform cameraTransform = Camera.main.transform; bool grounded = IsGrounded();& // Forward vector relative to the camera along the x-z plane Vector3 forward = cameraTransform.TransformDirection(Vector3.forward); forward.y = 0; forward = forward.normalized;& // Right vector relative to the camera // Always orthogonal to the forward vector Vector3 right = new Vector3(forward.z, 0, -forward.x);& float v = Input.GetAxisRaw("Vertical"); float h = Input.GetAxisRaw("Horizontal");& // Are we moving backwards or looking backwards if (v & -0.2f)
movingBack = true; else
movingBack = false;& bool wasMoving = isMoving; isMoving = Mathf.Abs (h) & 0.1f || Mathf.Abs (v) & 0.1f;& // Target direction relative to the camera Vector3 targetDirection = h * right + v * forward;& if(JFConst.TouchIng()) {
Vector3 vpos3 = Camera.main.WorldToScreenPoint(transform.position);
Vector2 vpos2 = new Vector2 (vpos3.x,vpos3.y);
Vector2 input = new Vector2 (Input.mousePosition.x,Input.mousePosition.y);
if(Vector2.Distance(vpos2,input) & 10.0f)
Vector2 normalied =
((vpos2 - input)).normalized;
targetDirection = new Vector3(normalied.x,0.0f,normalied.y) ;
float y = Camera.main.transform.rotation.eulerAngles.y;
targetDirection = Quaternion.Euler(0f,y - 180,0f) * targetDirection; &
} } & // Grounded controls if (grounded) {
// Lock camera for short period when transitioning moving & standing still
lockCameraTimer += Time.deltaTime;
if (isMoving != wasMoving)
lockCameraTimer = 0.0f;&
// We store speed and direction seperately,
// so that when the character stands still we still have a valid forward direction
// moveDirection is always normalized, and we only update it if there is user input.
if (targetDirection != Vector3.zero)
// If we are really slow, just snap to the target direction
if (moveSpeed & walkSpeed * 0.9f && grounded)
moveDirection = targetDirection.normalized;
// Otherwise smoothly turn towards it
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);&
moveDirection = moveDirection.normalized;
// Smooth the speed based on the current target direction
float curSmooth = speedSmoothing * Time.deltaTime;&
// Choose target speed
//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
float targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);&
_characterState = CharacterState.Idle;&
// Pick speed modifier
if (Input.GetKey (KeyCode.LeftShift) | Input.GetKey (KeyCode.RightShift))
targetSpeed *= runSpeed;
_characterState = CharacterState.Running;
else if (Time.time - trotAfterSeconds & walkTimeStart)
targetSpeed *= trotSpeed;
_characterState = CharacterState.Trotting;
targetSpeed *= walkSpeed;
_characterState = CharacterState.Walking;
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);&
// Reset walk time start when we slow down
if (moveSpeed & walkSpeed * 0.3f)
walkTimeStart = Time.time; } // In air controls else {
// Lock camera while in air
if (jumping)
lockCameraTimer = 0.0f;&
if (isMoving)
inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration; }&}&void ApplyJumping (){ // Prevent jumping too fast after each other if (lastJumpTime + jumpRepeatTime & Time.time)
return;& if (IsGrounded()) {
// - Only when pressing the button down
// - With a timeout so you can press the button slightly before landing
if (canJump && Time.time & lastJumpButtonTime + jumpTimeout) {
verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight);
SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
} }}&void ApplyGravity (){ if (isControllable) // don't move player at all if not controllable. {
// Apply gravity
bool jumpButton = Input.GetButton("Jump");&
// When we reach the apex of the jump we send out a message
if (jumping && !jumpingReachedApex && verticalSpeed &= 0.0f)
jumpingReachedApex = true;
SendMessage("DidJumpReachApex", SendMessageOptions.DontRequireReceiver);
if (IsGrounded ())
verticalSpeed = 0.0f;
verticalSpeed -= gravity * Time.deltaTime; }}&float CalculateJumpVerticalSpeed (float targetJumpHeight){ // From the jump height and gravity we deduce the upwards speed // for the character to reach at the apex. return Mathf.Sqrt(2 * targetJumpHeight * gravity);}&void&&DidJump (){ jumping = true; jumpingReachedApex = false; lastJumpTime = Time.time; lastJumpStartHeight = transform.position.y; lastJumpButtonTime = -10;& _characterState = CharacterState.Jumping;}&void&&Update() {& if (!isControllable) {
// kill all inputs if not controllable.
Input.ResetInputAxes(); }& if (Input.GetButtonDown ("Jump")) {
lastJumpButtonTime = Time.time; }& UpdateSmoothedMovementDirection();& // Apply gravity // - extra power jump modifies gravity // - controlledDescent mode modifies gravity ApplyGravity ();& // Apply jumping logic ApplyJumping ();& // Calculate actual motion Vector3 movement = moveDirection * moveSpeed + new Vector3 (0, verticalSpeed, 0) + inAirVelocity; movement *= Time.deltaTime;& // Move the controller CharacterController controller = GetComponent&CharacterController&(); collisionFlags = controller.Move(movement);& // ANIMATION sector if(_animation) {
if(_characterState == CharacterState.Jumping)
if(!jumpingReachedApex) {
_animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
_animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
_animation.CrossFade(jumpPoseAnimation.name);
_animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
_animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
_animation.CrossFade(jumpPoseAnimation.name);
if(controller.velocity.sqrMagnitude & 0.1f) {
_animation.CrossFade(idleAnimation.name);
if(_characterState == CharacterState.Running) {
_animation[runAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f, runMaxAnimationSpeed);
_animation.CrossFade(runAnimation.name);
else if(_characterState == CharacterState.Trotting) {
_animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
_animation.CrossFade(walkAnimation.name);
else if(_characterState == CharacterState.Walking) {
_animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
_animation.CrossFade(walkAnimation.name);
} } // ANIMATION sector& // Set rotation to the move direction if (IsGrounded()) {&
transform.rotation = Quaternion.LookRotation(moveDirection);& } else {
Vector3 xzMove = movement;
xzMove.y = 0;
if (xzMove.sqrMagnitude & 0.001f)
transform.rotation = Quaternion.LookRotation(xzMove);
} } & // We are in jump mode but just became grounded if (IsGrounded()) {
lastGroundedTime = Time.time;
inAirVelocity = Vector3.zero;
if (jumping)
jumping = false;
SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
} }}&void&&OnControllerColliderHit (ControllerColliderHit hit ){// Debug.DrawRay(hit.point, hit.normal); if (hit.moveDirection.y & 0.01f)
return;}&float GetSpeed () { return moveSpeed;}&public bool IsJumping () { return jumping;}&bool IsGrounded () { return (collisionFlags & CollisionFlags.CollidedBelow) != 0;}&Vector3 GetDirection () { return moveDirection;}&public bool IsMovingBackwards () { return movingBack;}&public float GetLockCameraTimer (){ return lockCameraTimer;}&bool IsMoving (){ return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) & 0.5f;}&bool HasJumpReachedApex (){ return jumpingReachedApex;}&bool IsGroundedWithTimeout (){ return lastGroundedTime + groundedTimeout & Time.time;}&void Reset (){ gameObject.tag = "Player";}&}
如此我们就可以通过鼠标与触摸牵引控制主角移动了,别急这紧紧是开始,下面是本章的要点。
这里我们分析一下摄像机到底什么时候该拉近,什么时候该不拉近。 我的作法是这样的,当角色移动的时候我会从主角身上向摄像机方向发射一条射线,如果射线碰撞到的第一个对象是“摄像机” 那么就标示主角和摄像机之间没有别的模型挡住,此时摄像机就不应该拉近。如果射线碰撞到的第一个对象不是摄像机,那么就标示主角和摄像机之间有别的模型所挡住,此时取得射线与别的模型碰撞的3D坐标接着将“摄像机”的坐标移动到这个坐标上即可。
如下图所示,(红色表示由主角发射到摄像机的射线)主角身后的射线没有被别的模型挡住,射线机不会拉近。
如下图所示,MOMO改变了一下主角的位置,此时主角身后的射向已经被墙挡住,这时把摄像机的坐标移动到被墙挡住点的坐标,这样摄像机就拉近了。
原理大概就是这样,接着我们学习一下这段代码该如何来写。
MyCamera.cs 挂在射线机上
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
using UnityEngine;using System.Collections;&public class MyCamera : MonoBehaviour{&&&&&//摄像机朝向的目标模型 public Transform target; //摄像机与模型保持的距离 public float distance = 10.0f; //射线机与模型保持的高度 public float height = 5.0f; //高度阻尼 public float heightDamping = 2.0f; //旋转阻尼 public float rotationDamping = 3.0f; //主角对象 private GameObject controller; & void Start() {
//得到主角对象
controller = GameObject.FindGameObjectWithTag("Player"); }& void Update() {& }& void LateUpdate () {
// Early out if we don't have a target
if (!target)
//当鼠标或者手指在触摸中时
if(JFConst.TouchIng())
bool follow = true; &&&&&&&&//计算相机与主角Y轴旋转角度的差。
float abs = Mathf.Abs(transform.rotation.eulerAngles.y - controller.transform.rotation.eulerAngles.y);
//abs等于180的时候标示摄像机完全面对这主角, 》130 《 230 表示让面对的角度左右偏移50度
//这样做是不希望摄像机跟随主角,具体效果大家把代码下载下来看看,这样的摄像机效果很好。
if(abs & 130 && abs & 230)
follow = false;
follow = true;
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;&
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;&
//主角面朝射线机 和背对射线机 计算正确的位置
if(follow)
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
Vector3 positon = target.position;
positon -= currentRotation * Vector3.forward * distance;
positon = new Vector3(positon.x,currentHeight,positon.z);
transform.position = Vector3.Lerp(transform.position,positon,Time.time);&
Vector3 positon = target.position;
Quaternion cr = Quaternion.Euler (0, currentRotationAngle, 0); &
positon += cr * Vector3.back * distance;
positon = new Vector3(positon.x,target.position.y + height,positon.z);
transform.position = Vector3.Lerp(transform.position,positon,Time.time);
&&&&//这里是计算射线的方向,从主角发射方向是射线机方向
Vector3 aim = target.position;
//得到方向
Vector3 ve = (target.position - transform.position).normalized;
float an = transform.eulerAngles.y;
aim -= an * ve ;
&&&&//在场景视图中可以看到这条射线
Debug.DrawLine(target.position,aim,Color.red);
&&&&//主角朝着这个方向发射射线
RaycastHit hit;
if(Physics.Linecast(target.position,aim,out hit))
string name =&&hit.collider.gameObject.tag;
if(name != "MainCamera" && name !="terrain")
//当碰撞的不是摄像机也不是地形 那么直接移动摄像机的坐标
transform.position = hit.point;&
// 让射线机永远看着主角
transform.LookAt (target); & }&}
牛头人被墙挡住了,摄像机直接拉近了,小牛头人是不是很帅气?哈哈哈!!
最后雨松MOMO把源码放出来,希望大家学习愉快,哇咔咔。。。
下载地址:
补充 :摄像机遮挡物体透明。
遮挡透明最简单的办法就是修改材质的透明度,前提需要把材质设置成支持透明通道的 shader Transparent 。 如下图所示,摄像机挡住的那面墙已经成透明状态了。
看看代码是如何写的,把上述代码MyCamera.cs 简单的改一下就可以。 主要是92行到101行。
代码核心就是通过射线得到碰撞模型的材质,然后修改材质的透明度。color.a就是透明度 1是完全不透明,0是完全透明。注意一定是支持透明通道的模型才可以。lastobj是记录上次透明的对象,用于模型透明还原使用。
123456789101112131415161718192021222324
if(Physics.Linecast(target.position,aim,out hit))
GameObject obj = hit.collider.gameObject;&
string name =&&obj.tag;
if(name != "MainCamera" && name !="terrain")
Color color = obj.renderer.material.color;
color.a = 0.5f;
obj.renderer.material.SetColor("_Color", color);&
lastobj = obj;&
if(lastobj != null)
Color color = lastobj.renderer.material.color;
color.a = 1f;
lastobj.renderer.material.SetColor("_Color", color);
最后,假设你的模型做的非常大,贴图材质也非常的多,你像透明其中一小部分那么上述方法就不合适了,除非把他们都拆出来。我觉得也可以自己写shader实现。我们的项目采用了摄像机拉近的方式。欢迎讨论!!!
回答楼下问题:上帝视角代码,看到楼下有朋友问我,那么我就贴出来,其实很简单。下面代码挂在摄像机上, target就是角色对象,distance 是摄像机与角色的距离 height是摄像机与角色的高度。在编辑器中手动调节即可。
1234567891011121314151617181920212223242526
using UnityEngine;using System.Collections;&public class ICamera : MonoBehaviour {&public Transform target;// The distance in the x-z plane to the targetpublic float distance = 10.0f;// the height we want the camera to be above the targetpublic float height = 5.0f;&void LateUpdate () { // Early out if we don't have a target if (!target)
return;& // Set the position of the camera on the x-z plane to: // distance meters behind the target transform.position = target.position; transform.position -=&&Vector3.forward * distance; transform.position = new Vector3(transform.position.x,transform.position.y + height,transform.position.z);& // Always look at the target transform.LookAt (target); }}
回答问题 :摄像机抖动如何修改
今天微薄上有个朋友问我这样的问题,我在这里解答一下。
来福12345:我又厚颜无耻的来了,您的《Unity3D的研究院之处理摄像机跟随避免相机穿墙拉近或透明的方法(四十四)》对于不规则的物体该怎么弄,我是直接在unity上弄了几个山脉,由于地面凹凸不平,沿着山走的时候发现摄像机会一直透视,而且抖动的很厉害,请问该怎么解决啊
1.从设计上避免射线遇到一些复杂的面,如下图所示,红颜色是主角身后的射线,当主角身后的射线碰撞到这样的面,如果此时移动速快快一些摄像机肯定会抖动。 这里最好直接删除Mesh Collider 换成BoxCollider 效率还可以得到提升。产经设计这块我觉得可以参考一下魔兽世界,我在项目中遇到这样问题的地方就是 比较小的门(棱角那种) 或者这样的树。
2.但是还有一些比较特殊的地方,如果你的项目必需使用MeshColider 的话 我建议你写触发脚本来修改摄像机,假设 当摄像机被挡住后,变成跟随视角,只到摄像机没被挡住的时候在回到原来视角。
3.我检查了一下上面的代码中我们在加一个判断,应该就可以解决大部分问题、在LateUpdate () 中最后加入代码
12345678910111213141516
if(!closer)
transform.position = positon;&
float dis = Vector3.Distance(transform.position,controller.transform.position);
if(dis & 8)
distance = dis;
closer = false;
bool closer 是表示是否拉近。 当摄像机被挡住的时候这个数值等于
12345678910
if(Physics.Linecast(tirggerTaget,aim,out hit))
string name =&&hit.collider.gameObject.tag;
if(name != "MainCamera")
closer = true;
transform.position = hit.point;
distance = Vector3.Distance(transform.position,controller.transform.position) - 2f;
当closer等于假的时候,此时直接修更新摄像机的位置,因为没被挡住。 当closer等于真的时候,摄像机已经被挡住了,我们不修改摄像机的位置,计算摄像机保证不被挡住时,摄像机此时和主角的距离是多少,然后修改distance全局变量即可。当摄像机不被遮挡的时候在恢复之前的距离。 如有问题请留言,我会即时解答。。 谢谢
//补充/////////
之前的代码还是有点问题,后来我又仔细的重构了一下摄像机的代码。下面的代码在我的项目中已经完美无暇的模拟摄像机拉近了。。 代码对外有两个公有类型, 一个是模型的 一个是模型位置 与头顶的偏移, 因为射线应该是重头顶向后发射的。
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
using UnityEngine;using System.Collections;using System.Threading;&public class PersonCamera : MonoBehaviour {& public const&&float DISTANCE_DEAFULT = 8.0f; private float distance = 0.0f; public Transform target; public float target_offsety = 1.8f; private Vector3 payerTaget; & void Awake() {
distance = DISTANCE_DEAFULT;
payerTaget =&&new Vector3(target.position.x,target.position.y + target_offsety , target.position.z);
Quaternion cr = Quaternion.Euler (30f, transform.eulerAngles.y, 0);
Vector3 positon = payerTaget;
positon += cr * Vector3.back * distance;
transform.position = positon;
transform.LookAt(payerTaget); } void LateUpdate() {&
payerTaget =&&new Vector3(target.position.x,target.position.y + target_offsety , target.position.z);
Quaternion cr = Quaternion.Euler (30f, transform.eulerAngles.y, 0);
Vector3 positon = payerTaget + (cr * Vector3.back * distance);
RaycastHit []hits = Physics.RaycastAll(new Ray(payerTaget,(positon -payerTaget).normalized));
distance = DISTANCE_DEAFULT;
if(hits.Length & 0)
RaycastHit stand = hits[0];
foreach(RaycastHit hit in hits)
if(hit.collider.gameObject.tag == "terrain")
if(hit.distance & stand.distance)
stand = hit;
Debug.Log(stand.point + " " +stand.collider.gameObject.tag);
string tag = stand.collider.gameObject.tag;
distance = Vector3.Distance(stand.point,payerTaget);
if(distance & DISTANCE_DEAFULT)
distance = DISTANCE_DEAFULT;
positon = payerTaget + (cr * Vector3.back * distance);
&&&&transform.position = Vector3.Lerp(transform.position,positon,0.3f);;&
transform.LookAt(payerTaget);&
Debug.DrawRay(payerTaget,positon -payerTaget,Color.red);
本文固定链接:
转载请注明:
雨松MOMO提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
您可能还会对这些文章感兴趣!
Pingback 引用通告:}

我要回帖

更多关于 unity3d第三人称视角 的文章

更多推荐

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

点击添加站长微信