第三人称镜头的脚本
public class Camera2 : MonoBehaviour {
float x_inputmouse;
float y_inputmouse;
public float turnspeed;
public Transform Player;
public Transform eye;
void LateUpdate()
{
transform.position = eye.position;
x_inputmouse = Player.eulerAngles.y;
y_inputmouse += Input.GetAxis("Mouse Y") * turnspeed * Time.deltaTime;
y_inputmouse = Mathf.Clamp(y_inputmouse, -40, 80);
Quaternion Angle = Quaternion.Euler(-y_inputmouse, x_inputmouse, 0);
this.transform.rotation = Angle;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraLenth : MonoBehaviour {
Vector3 mousescroll;
float scroll;
Ray ray;
public Transform Player;
RaycastHit hitPoint;
bool hit;
public Vector3 original;
private void Start()
{
original =transform.localPosition;
}
void LateUpdate () {
Debug.Log("localPosition的值为" + transform.localPosition);
int layerMask = 1 << 10;
layerMask = ~layerMask;
ray.origin = transform.position;
ray.direction = transform.forward;
hit=Physics.Raycast(ray, out hitPoint, Vector3.Distance(transform.position, Player.position),layerMask);
if(hit)
{
transform.localPosition = transform.localPosition*0.9f;
}
scroll =-Input.GetAxis("Mouse ScrollWheel")+1;
scroll = scroll * scroll;
transform.localPosition = transform.localPosition*scroll;
if(transform.localPosition.z>-1&&scroll<1)
{
transform.localPosition = transform.localPosition / scroll;
}
if(transform.localPosition.z>-0.5)
{
transform.localPosition *= 1.1f;
}
Debug.Log(original);
}
}