transform.rotation * new Vector3 (1, 0, 0)

 


 



总结:

transform.rotation * new Vector3 (1, 0, 0) : 该脚本对象局部x轴所代表的全局方向向量

transform.rotation * new Vector3 (0, 1, 0) : 该脚本对象局部y轴所代表的全局方向向量

transform.rotation * new Vector3 (0, 0, 1) : 该脚本对象局部z轴所代表的全局方向向量

 



 

 

FR:海涛高软(hunk Xu)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断右键是否按下 if (Input.GetMouseButton(1)) { 获取鼠标在水平和垂直方向上的移动距离 float mouseX = Input.GetAxis(“Mouse X”); float mouseY = Input.GetAxis(“Mouse Y”); currentRotationY += mouseX * rotationSpeed; currentRotationX -= mouseY * rotationSpeed; float targetRotationY = currentRotationY + mouseX * rotationSpeed; float targetRotationX = currentRotationX - mouseY * rotationSpeed; // 限制上下旋转的角度 currentRotationX = Mathf.Clamp(currentRotationX, minYAngle, maxYAngle); // 使用平滑插值逐渐改变当前旋转角度到目标旋转角度 currentRotationY = Mathf.Lerp(currentRotationY, targetRotationY, rotationSmoothness * Time.deltaTime); currentRotationX = Mathf.Lerp(currentRotationX, targetRotationX, rotationSmoothness * Time.deltaTime); float targetDistance = Mathf.Lerp(0.5f, distance, (currentRotationX - minYAngle) / (maxYAngle - minYAngle)); float newDistance = Mathf.Lerp(distance, targetDistance, rotationSmoothness * Time.deltaTime); transform.position = target.position - transform.forward * newDistance; // 设置摄像机的旋转 Quaternion yRotation = Quaternion.Euler(0, currentRotationY, 0); Quaternion xRotation = Quaternion.Euler(currentRotationX, 0, 0); Quaternion rotation = initialRotation * yRotation * xRotation; transform.rotation = rotation; }Vector3 direction =this.transform.position- target.position; Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); Vector3 position = transform.rotation * negDistance + target.position; //Vector3 vertexPosition = target.position + target.up * distance; // // 从玩家的位置沿着朝向摄像机发出射线 Ray ray = new Ray(target.position, direction.normalized); if (Physics.Raycast(ray, out hit,distance)) { //Debug.DrawRay(target.position, vertexPosition, Color.red); if (hit.collider.gameObject != target.gameObject) { transform.position = transform.rotation* new Vector3(0.0f, 0.0f, -Vector3.Distance(hit.point, target.position)) + target.position; //transform.position=hit.point; } else { //transform.position = position; } } else { transform.position = position; }这段代码摄像机会一会正常位置,一会到玩家身上,怎么修改
07-13
根据你提供的代码,我看到你在鼠标右键按下时进行了一系列操作来控制摄像机的旋转和位置。根据你的描述,你希望摄像机在玩家身上时保持固定的位置,而不是跟随玩家移动。 在你的代码中,当射线检测到与玩家对象不同的物体时,你将摄像机的位置设置为射线检测到的点与玩家位置之间的距离。这可能导致摄像机在一些情况下跳到玩家身上。 要解决这个问题,你可以修改代码来实现以下逻辑: 1. 在鼠标右键按下时进行旋转和位置的计算。 2. 在射线检测到与玩家对象不同的物体时,只更新摄像机的位置为射线检测到的点与玩家位置之间的距离。 3. 在射线未检测到任何物体时,更新摄像机的位置为固定的位置。 修改后的代码如下所示: ```csharp if (Input.GetMouseButton(1)) { // 进行旋转计算 // ... Vector3 direction = this.transform.position - target.position; Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); Vector3 position = transform.rotation * negDistance + target.position; Ray ray = new Ray(target.position, direction.normalized); if (Physics.Raycast(ray, out hit, distance)) { if (hit.collider.gameObject != target.gameObject) { // 只更新摄像机的位置,不进行旋转 transform.position = transform.rotation * new Vector3(0.0f, 0.0f, -Vector3.Distance(hit.point, target.position)) + target.position; } } else { // 没有检测到物体时,更新摄像机的位置为固定位置 transform.position = position; } } ``` 请注意,这只是一种可能的修改方法。根据你的具体需求和场景,你可能需要进行更多的调整来实现你想要的效果。希望这可以帮助到你!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值